v0.6.22

i18n

Last updated


This page covers defining locales, referencing translations in text and attributes, and switching languages.

Defining Locales


  1. <div id="app">
  2. <h1>{{ i18n.title }}</h1>
  3. <input placeholder="i18n.search" />
  4. <button @click="toEn">English</button>
  5. </div>
  6. <script>
  7. const app = new QUI({
  8. id: "app",
  9. i18n: {
  10. zh: { title: "歡迎", search: "搜尋" },
  11. en: "/i18n/en.json",
  12. },
  13. i18nLang: "zh",
  14. event: {
  15. toEn: () => app.lang("en"),
  16. },
  17. });
  18. </script>
Live preview · @pardnchiu/quickui@0.6.22

OptionDescription
i18nMaps a locale code to an object, or to the URL of a JSON file
i18nLangInitial locale, defaults to "zh"

String values are fetched before the initial render; when loading fails or the response is not 2xx, that locale becomes an empty object and rendering continues.

Referencing Translations


LocationSyntaxKey rulesWhen missing
Text{{ i18n.menu.home }}Nested keys with .Shows the key itself (menu.home)
Attribute valueplaceholder="i18n.search"Single-level keys onlyKeeps the original string i18n.search

The attribute value must be exactly i18n.key with no other text mixed in.

Switching Languages


app.lang(code) updates the current locale and triggers a re-render; it does nothing when code is not in i18n. Instances created with once: true have non-reactive data, so switching does not re-render.

Reserved Keys


Locale data lives in app.data.quickui_i18n and the current locale in app.data.quickui_i18nLang. Do not use these two names in your own data.
中文