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.languagestarts as thelanguageURL hook oren. The default view then uses the hook, the user'slanguageoren, in that order.[1] - Dictionaries. English is bundled from
public/dictionaries/en.mjs.mapp.utils.loadDictionary(code)fetches${mapp.host}/public/dictionaries/<code>.jsonand merges it intomapp.dictionaries[code].[2] The repository hasde,es,fr,it,ja,ko,pl,th,tr,zhandzh_tw. - Lookup.
mapp.dictionary.<key>returns the text inmapp.language, else the English text, else the key itself. Ifmapp.languagehas no dictionary, the lookup warns and setsmapp.languagetoen.[3] - Language templates. A template in
workspace.templatesmay be an object of language codes, eg.{ "en": "file:/views/report_en.html", "de": "file:/views/report_de.html" }. The view API picks thelanguageparam's entry, oren; anenentry is required. A value with a provider prefix such asfile:orhttps:is loaded as a src.[4] Mail and message templates (_msgs.js) work the same way. - Workspace values.
keyvalue_dictionaryon a locale or layer is a list of{ key, value, default, <code>: text }. Every string propertykeywith the valuevalueis replaced with the text for the user's language,mapp.language, ordefault.[5] The mapview applies the locale's list,layer.decoratethe 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.mjsand the<code>.jsonfiles. Use the key in code asmapp.dictionary.<key>. - Add a language: a new
<code>.json, plus the language lists in_login.htmland_user.html. - Server messages:
apps/xyz/mod/workspace/templates/_msgs.jsand_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.languageand awaitloadDictionarybefore creating ui elements. Elements created earlier keep their English texts. - The view API accepts two letter codes only.
language=zh_twloads thezh_twdictionary in the browser, but the HTML getslang="en"and theenview template. keyvalue_dictionaryprefers the user's stored language over thelanguageURL hook, while the default view prefers the hook.