function I18n()
Create a translation manager for localized strings. The returned object keeps a dictionary by language, resolves nested keys, and can add or merge new translations.
- Description:
function I18n()
Create a translation manager for localized strings. The returned object keeps a dictionary by language, resolves nested keys, and can add or merge new translations.
- Source:
Properties:
| Name | Type | Description |
|---|---|---|
dict |
object | translation dictionary by language code |
language |
string | current language code |
languageDefault |
string | fallback language used when language is not available |
setLanguage(value) |
function | set the active language, lower-cased if necessary |
getLanguage() |
function | return the active language code |
t(key) |
function | return a translated string or the last path segment as fallback |
tPath(path) |
function | create a namespaced translator for keys under a common path |
addWords(words, |
function | lang, chapter) - merge translations into the dictionary |
Example
import i18n from 'vanjs-bootstrap/i18n; // import extra module
const I18n = i18n(); // create instance
I18n.addWords({ hello: 'Hello' }, 'en');
I18n.addWords({ hello: 'Hola' }, 'es');
I18n.addWords({ hello: '你好' }, 'zh');
I18n.addWords({ hello: 'Hallo' }, 'de');
I18n.setLanguage('de');
console.log(I18n.t('hello')); // -> Hallo
Returns:
- Type
- object