Class: I18n

I18n()

new I18n()

internationalization module
Source:

Members

(private, static) language

built in language
Properties:
Type Description
string | null
Source:

(private, static, readonly) languageLoadedEvent

const for string of loaded language
Properties:
Type Description
string
Source:

(private, static, readonly) supportedLanguages

supported languages, minus the built in 'en'
Properties:
Type Description
Array.<string>
Source:

(private, static) translations :Object

translation cache
Type:
  • Object
Source:

Methods

(static) _(element, messageId, …args) → {string}

translate a string, alias for I18n.translate
Parameters:
Name Type Attributes Description
element HTMLElement optional
messageId string
args * <repeatable>
one or multiple parameters injected into placeholders
Source:
Returns:
Type
string

(static) getLanguage() → {string}

get currently loaded language
Source:
Returns:
Type
string

(static) getPluralForm(n) → {int}

per language functions to use to determine the plural form
Parameters:
Name Type Description
n int
Source:
See:
Returns:
array key
Type
int

(private, static) isStringContainsHtml(messageId) → {boolean}

Check if string contains valid HTML code. If it is no string at all, this always returns false for compatibility.
Parameters:
Name Type Description
messageId string | object
Source:
Returns:
Type
boolean

(static) loadTranslations()

load translations into cache
Source:

(static) onLanguageLoaded(callback)

Register a callback to be invoked when translations have been loaded. This is useful for code that needs to re-apply translated strings to DOM attributes (e.g. title attributes) that cannot be handled by I18n.translate's built-in element re-translation mechanism (which only sets textContent/innerHTML). Note: If translations are already loaded when this is called, the callback will not be invoked. Callers should apply the translation immediately as well, to cover the case where the language is already available.
Parameters:
Name Type Description
callback function function to call when language is loaded
Source:

(static) reset()

resets state, used for unit testing
Source:

(static) translate(element, messageId, …args) → {string}

translate a string Optionally pass an HTMLElement as the first parameter, to automatically let the text of this element be replaced. In case the (asynchronously loaded) language is not downloaded yet, this will make sure the string is replaced when it eventually gets loaded. Using this is both simpler and more secure, as it avoids potential XSS when inserting text. The next parameter is the message ID, matching the ones found in the translation files under the i18n directory. Any additional parameters will get inserted into the message ID in place of %s (strings) or %d (digits), applying the appropriate plural in case of digits. See also Helper.sprintf().
Parameters:
Name Type Attributes Description
element HTMLElement optional
messageId string
args * <repeatable>
one or multiple parameters injected into placeholders
Source:
Returns:
Type
string