# Internationalization

The library has built-in support for Chinese and English. To support other languages, custom configuration is required.

Call the `getI18n` function to get the i18n object, then use the `setMsgs` method to set message templates for a language.

```ts
const i18n = getI18n()
i18n.setMsgs('ja', {
  "cancel": "キャンセル",
  "confirm": "確認",
  "choose-file": "ファイルを選択してください"
  "form-err-required": "この項目を入力してください",
  "form-err-must-check": "この項目をチェックしてください",
  "form-err-number": "数字を入力してください",
  "form-err-min": "{} より小さい値を入力しないでください",
  "form-err-max": "{} より大きい値を入力しないでください",
  "form-err-max-select": "{} 個以内のオプションを選択してください",
  "form-err-min-select": "{} 個以上のオプションを選択してください",
  "form-err-max-length": "{} 文字以内で入力してください",
  "form-err-min-length": "{} 文字以上で入力してください",
  "form-err-min-size": "ファイルのサイズは {} より小さくならないでください",
  "form-err-max-size": "ファイルのサイズは {} より大きくならないでください",
  "form-err-max-files-select": "{} 個以内のファイルを選択してください",
  "form-err-min-files-select": "{} 個以上のファイルを選択してください"
})
// You can also set up async loading so messages are loaded on demand, reducing bundle size
i18n.setMsgs('ja', () => fetch('/wok-ui/i18n/ja.json').then(res => res.json()))
```

After configuring a custom language, call the i18n object's `setLang` to switch to the appropriate language.

```ts
getI18n().setLang('tibt')
```

`setLang` is asynchronous and returns a `Promise` — this is to handle async language loading. It's best to wait for `setLang` to complete before initializing the page, ensuring the page renders with the correct language.

### Internationalization Extension

In actual development, if your project needs internationalization, you can extend wok-ui's i18n. This keeps the project's language consistent with the framework.

The i18n object returned by `getI18n` has an `extend` method that creates a new extended i18n object. When the i18n object's language changes, the extended object also changes. You can set project-specific message templates on the extended object.

Below is a complete code demonstration:

```ts
/**
 * Extended i18n messages
 */
interface ExtI18nMsgs {
  currentLanguage: string
  switchToCustomLanguage: string
  internationalization: string
}

/**
 * Extended i18n global object
 */
let extI18n: I18n<ExtI18nMsgs> | undefined = undefined

/**
 * Initialize i18n. Set up all languages supported by the project and switch to the appropriate one.
 * The project must wait for i18n initialization to complete before processing pages,
 * otherwise the page may render in the wrong language.
 */
export async function initI18n() {
  if (extI18n) {
    throw new Error(`Initialization has finished !`)
  }
  const i18n = getI18n()
  // Add a new language to the base library, set as async loading
  i18n.setMsgs('Tibt', () => fetch('/wok-ui/i18n/tibt.json').then(res => res.json()))

  // Load the default language first. In actual development, the default language
  // can also be bundled directly in the code file to save a request.
  // Either way, default language data must exist — one extra network request is more expensive.
  const enUs = await fetch('/wok-ui/i18n/ext-en-us.json').then(res => res.json())

  // Extend to create a new i18n object. Must set the English message template.
  // The i18n component defaults to English, so the extended i18n object stays consistent with the original.
  extI18n = i18n.extend<ExtI18nMsgs>(enUs)

  // Set up async loading for other languages in the extension
  extI18n.setMsgs('zh-CN', () => fetch('/wok-ui/i18n/ext-zh-cn.json').then(res => res.json()))
  extI18n.setMsgs('tibt', () => fetch('/wok-ui/i18n/ext-tibt.json').then(res => res.json()))

  // After setting up all languages, switch to the appropriate one.
  // Here we use the browser's language list. In actual projects, you might use the user's platform language setting.
  const supportedLangs = i18n.getSupportedLanguageTags(...navigator.languages)
  if (supportedLangs.length) {
    await i18n.setLang(supportedLangs[0])
  }
}
/**
 * Get the extended i18n object
 */
export function getExtI18n(): I18n<ExtI18nMsgs> {
  if (!extI18n) {
    throw new Error(`Please invoke initI18n() first !`)
  }
  return extI18n
}
```
