Skip to content

Language and dictionaries ​

Texts are translated in two places. The XYZ API picks a language template for a view, mail or message. In the browser, MAPP looks interface texts up in a dictionary for mapp.language.

Behaviour ​

  • Language code. mapp.language starts as the language URL hook or en. The default view then uses the hook, the user's language or en, in that order.[1]
  • Dictionaries. English is bundled from public/dictionaries/en.mjs. mapp.utils.loadDictionary(code) fetches ${mapp.host}/public/dictionaries/<code>.json and merges it into mapp.dictionaries[code].[2] The repository has de, es, fr, it, ja, ko, pl, th, tr, zh and zh_tw.
  • Lookup. mapp.dictionary.<key> returns the text in mapp.language, else the English text, else the key itself. If mapp.language has no dictionary, the lookup warns and sets mapp.language to en.[3]
  • Language templates. A template in workspace.templates may be an object of language codes, eg. { "en": "file:/views/report_en.html", "de": "file:/views/report_de.html" }. The view API picks the language param's entry, or en; an en entry is required. A value with a provider prefix such as file: or https: is loaded as a src.[4] Mail and message templates (_msgs.js) work the same way.
  • Workspace values. keyvalue_dictionary on a locale or layer is a list of { key, value, default, <code>: text }. Every string property key with the value value is replaced with the text for the user's language, mapp.language, or default.[5] The mapview applies the locale's list, layer.decorate the layer's.

Flow ​

URL language → view API and the language template → the browser: mapp.mjs sets mapp.language → the view's script sets it again and awaits loadDictionary → mapp.Mapview loads a missing dictionary before it runs plugins → ui elements read mapp.dictionary.

Where to change ​

  • Add or correct a text: public/dictionaries/en.mjs and the <code>.json files. Use the key in code as mapp.dictionary.<key>.
  • Add a language: a new <code>.json, plus the language lists in _login.html and _user.html.
  • Server messages: apps/xyz/mod/workspace/templates/_msgs.js and _mails.js.

Tests ​

apps/mapp/tests/dictionary.test.mjs covers the fallbacks; utils/loadDictionary.test.mjs and utils/keyvalue_dictionary.test.mjs the utilities; apps/xyz/tests/mod/utils/languageTemplates.test.mjs the template selection.

Pitfalls ​

  • Set mapp.language and await loadDictionary before creating ui elements. Elements created earlier keep their English texts.
  • The view API accepts two letter codes only. language=zh_tw loads the zh_tw dictionary in the browser, but the HTML gets lang="en" and the en view template.
  • keyvalue_dictionary prefers the user's stored language over the language URL hook, while the default view prefers the hook.

  1. _default.html and mapp.mjs ↩︎

  2. utils/loadDictionary.mjs ↩︎

  3. dictionary.mjs get ↩︎

  4. utils/languageTemplates.js ↩︎

  5. utils/keyvalue_dictionary.mjs ↩︎

XYZ v5.1.0. Generated from knowledge/ at dd7a6fa27736502733499c1bf485f15659b10338. Concepts marked draft are not reviewed.