I18N

1.0.0+

Class for accessing internationalization (i18n) features.

Allows “detecting language change”, “setting the current language” or translating “individual terms”.

Internationalization works with terms, a string type keyword that is linked to a different text for each language.

Internally, string parameters for text and js components are automatically swapped during language change, given they are linked to a term. If manual text swapping is desired, translate can be used to retrieve the current translation for any term.

You can also use the onLanguageChanged to manually update text when a language is changed to for example update a number in a string.

.constructor(engine: WonderlandEngine) ⇒ I18N 

Constructor

ParamTypeDescription
engineWonderlandEngine

.onLanguageChanged: Emitter<[number, number]> 

Emitter for language change events.

First parameter to a listener is the old language index, second parameter is the new language index.

Usage from a within a component:

1this.engine.i18n.onLanguageChanged.add((oldLanguageIndex, newLanguageIndex) => {
2    const oldLanguage = this.engine.i18n.languageName(oldLanguageIndex);
3    const newLanguage = this.engine.i18n.languageName(newLanguageIndex);
4    console.log("Switched from", oldLanguage, "to", newLanguage);
5});

.currentIndex: number 

Get the current language index.

This method is more efficient than its equivalent:

1const index = i18n.languageIndex(i18n.language);

.engine: WonderlandEngine 

Hosting engine instance.

.language: null | string 

Get current language code.

.language 

Set current language and apply translations to linked text parameters.

Note: This is equivalent to setLanguage.

.previousIndex: number 

Previous language index.

.languageCode(index: number) ⇒ null | string 

Get a language code.

ParamTypeDescription
indexnumberIndex of the language to get the code from

.languageCount() ⇒ number 

Get the number of languages in the project.

.languageIndex(code: string) ⇒ number 

Get a language code.

ParamTypeDescription
codestring

.languageName(index: number) ⇒ null | string 

Get a language name.

ParamTypeDescription
indexnumberIndex of the language to get the name from

.setLanguage(code: null | string) ⇒ Promise<number

Set current language and apply translations to linked text parameters.

Returns: A promise that resolves with the current index code when the language is loaded.

ParamTypeDescription
codenull | stringThe language code.

.translate(term: string) ⇒ null | string 

Get translated string for a term for the currently loaded language.

ParamTypeDescription
termstringTerm to translate