# misc/localization (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/localization/

Localization.

## Localization extends Object

Localization.
- Gathers automatic language detection + translation tables + {0}{1} placeholder substitution + fallback in one place.
- Keys may be the source text as is. If missing from the table, the source text (key) is returned as is, so translation can be gradual.
  Usage example:
    Localization.setTable("en", { "Confirm": "OK", "{0} points earned": "Got {0} points" });
    Localization.setLanguage(Localization.detectLanguage(["ko", "en"]));
    Localization.text("{0} points earned", 120);
- Matches a language tag by dropping trailing subtags one at a time, and also checks aliases. (setLanguageAliases)
- For a right-to-left language (isRightToLeft, setRightToLeft), mirrors the layout (mirrorLayoutX, readLayoutTextAlign),
  and wraps values inserted in text with directional isolate characters. (isolateText, the "{name}" placeholders of textNamed, values joined only by slashes are wrapped once as a whole)

Example: Add translation tables, choose a language and get text

```js
import { Localization } from "./build/vanilla.js";

Localization.setTable("en", { "greeting": "Hello", "score": "Got {0} points", "restart": "Restart in {seconds}" });
Localization.setTable("fr", { "greeting": "Bonjour", "score": "{0} points obtenus" });
Localization.setFallbackLanguage("en");
const languageCode = Localization.selectLanguage(["fr-CA", "en-US"], ["en", "fr"], "en");
Localization.setLanguage(languageCode);
console.log(languageCode, Localization.text("greeting"), Localization.text("score", 120));
console.log(Localization.textNamed("restart", { seconds: 3 }));
```

- `static detectLanguage(supportedLanguageCodes, defaultLanguageCode = "ko")` (static, since 0.3.0): Detects the browser language. (static)
  - Scans navigator.languages in priority order and picks the first language in the supported list. (selectLanguage)
    A regional tag like "zh-TW" is matched in the order "zh-tw" → "zh".
  - `supportedLanguageCodes` string[]: List of supported language codes. (case-insensitive)
  - `defaultLanguageCode` string: Code returned when nothing matches.
  - returns string: The chosen language code. (lowercase if found, otherwise the default code as is)
- `static selectLanguage(candidateList, supportedLanguageCodes, defaultLanguageCode = "ko")` (static, since 0.4.0): Picks the first supported language from a preferred language list. (static, the matching part of detectLanguage)
  - Drops trailing subtags of the tag one at a time and checks the codes and aliases in the supported list. ("zh-Hant-TW" → "zh-hant-tw", "zh-hant", "zh", underscores become hyphens)
  - `candidateList` string[]: Language tags in order of preference. (navigator.languages form)
  - `supportedLanguageCodes` string[]: List of supported language codes. (case-insensitive)
  - `defaultLanguageCode` string: Code returned when nothing matches.
  - returns string: The chosen language code. (lowercase if found, otherwise the default code as is)

  Example: Match regional tags with aliases

  ```js
  import { Localization } from "./build/vanilla.js";
  
  Localization.setLanguageAliases("zh-hant", ["zh-tw", "zh-hk"]);
  const languageCode = Localization.selectLanguage(["zh-TW", "en-US"], ["en", "zh-hant"], "en");
  console.log(languageCode);
  ```

- `static setLanguageAliases(languageCode, aliasList)` (static, since 0.4.0): Sets aliases for a language. (static, e.g. "zh-tw", "zh-hk" for "zh-hant", aliases also count as that language when matching tags)
  - `languageCode` string: Language code the aliases point to.
  - `aliasList` string[]: List of alias tags. (lowercased, underscores become hyphens)
- `static setRightToLeft(languageCode, isRightToLeft)` (static, since 0.4.0): Sets or unsets a language as right-to-left. (static, initially ar, fa, he, ur, ps, yi, dv)
  - `languageCode` string: Language code or tag. (only the first subtag is used)
  - `isRightToLeft` boolean: True if written right to left. (removed from the list if false)
- `static isRightToLeft()` (static, since 0.4.0): Whether the current language is right-to-left. (static, for baking text in that direction and mirroring the layout horizontally)
  - Like setRightToLeft, it checks the first part read in lowercase with underscores as hyphens. ("ar_EG" is ar too)
  - returns boolean: True if the first subtag of the current language is in the right-to-left list.
- `static mirrorLayoutX(x, width, parentWidth)` (static, since 0.4.0): Horizontal position of the layout. (static, for a right-to-left language the left mirrored within the parent width, otherwise unchanged)
  - Used to mirror the left and right placement of windows and tables to match the language direction. Not called for things that should not be mirrored, like screen corner positions or slider direction.
  - `x` number: Left for a left-to-right language.
  - `width` number: Width of the thing being placed.
  - `parentWidth` number: Width of the parent.
  - returns number: Left position matched to the current language direction.
- `static readLayoutTextAlign(textAlign)` (static, since 0.4.0): Text alignment of the layout. (static, for a right-to-left language swaps "left" and "right", others unchanged)
  - `textAlign` string: Value of TextAlign.
  - returns string: Alignment value matched to the current language direction.
- `static isolateText(text)` (static, since 0.4.0): Wraps a value inserted in text. (static, for a right-to-left language wraps it with directional isolate characters, otherwise unchanged)
  - Keeps numbers with units (123°/s, +60%) or people's names placed as one chunk in their own direction, instead of mixing and scattering with the surrounding text.
  - `text` string: Text of the value to wrap.
  - returns string: Text wrapped in directional isolate characters for right-to-left languages, otherwise unchanged. (empty text is unchanged)
- `static textNamed(key, values)` (static, since 0.4.0): Fills named placeholders. (static, "Restart {seconds}" + { seconds: 3 } → "Restart 3")
  - Fills after finding the translation (text). For a right-to-left language, wraps the inserted values with directional isolate characters.
  - Values joined only by slashes ("{count}/{limit}") are wrapped once as a whole, not per value. (wrapping per value makes 1/40 look like 40/1)
  - Placeholders with no matching name are left as is.
  - `key` string: Translation key. (source text)
  - `values` object: Placeholder name → value.
  - returns string: The translated text with placeholders filled.

  Example: Wrap values in a right-to-left language

  ```js
  import { Localization } from "./build/vanilla.js";
  
  Localization.setTable("ar", { "progress": "{count}/{limit}" });
  Localization.setLanguage("ar");
  const progressText = Localization.textNamed("progress", { count: 1, limit: 40 });
  console.log(Localization.isRightToLeft(), progressText.length);
  Localization.setLanguage("en");
  ```

- `static setTable(languageCode, table)` (static, since 0.3.0): Sets a translation table. (static)
  - `languageCode` string: Language code. (stored lowercased)
  - `table` object: Key (source text) → translation. The value is a string or a string array.
- `static setTableFromJson(languageCode, tableJson)` (static, since 0.4.0): Sets a translation table from a JSON object. (static, such as data read with JsonAsset, a string value as a string, a string array as a list of only its strings, other values skipped)
  - `languageCode` string: Language code. (stored lowercased)
  - `tableJson` object: JSON object of key → text or text array. (an empty table if not an object)
- `static setLanguage(languageCode)` (static, since 0.3.0): Sets the current language. (static)
  - `languageCode` string: Language code. (stored lowercased)
- `static getLanguage()` (static, since 0.3.0): Returns the current language. (static)
  - returns string: The current language code. (lowercase, "ko" at first)
- `static setFallbackLanguage(languageCode)` (static, since 0.3.0): Sets the fallback language. (static, looks in this language's table when missing from the current language's table)
  - `languageCode` string: Fallback language code. (stored lowercased, "ko" at first)
- `static text(key, ...substitutions)` (static, since 0.3.0): Looks up a translation. (static)
  - Searches in the order current language → fallback language → key source text.
  - Substitutes the {0} {1} … placeholders with the arguments.
  - `key` string: Translation key. (source text)
  - `...substitutions` ...*: Values put into {0}, {1} in order. (converted with String)
  - returns string: The translated text. If not in the table, the key text with placeholders filled.
- `static textList(key)` (static, since 0.3.0): Looks up an array translation. (static, for multi-line entries like dialogue lists)
  - `key` string: Translation key. (source text)
  - returns string[]: List of translated texts. (an array with just that text if it is a single string, [key] if missing)
- `static lookup(key)` (static, since 0.3.0): Raw lookup. (static)
  - `key` string: Translation key. (source text)
  - returns string | string[] | undefined: Value found in the current language table, then the fallback table. undefined if missing.
- `static clear()` (static, since 0.3.0): Clears all tables. (static, for testing)
