Class: I18n

pc.I18n

Handles localization. Responsible for loading localization assets and returning translations for a certain key. Can also handle plural forms. To override its default behaviour define a different implementation for pc.I18n#getText and pc.I18n#getPluralText.

Constructor

(private) new I18n(app)

Parameters:
Name Type Description
app pc.Application The application.
Properties:
Name Type Description
locale String The current locale for example "en-US". Changing the locale will raise an event which will cause localized Text Elements to change language to the new locale.
assets Array.<Number> | Array.<pc.Asset> An array of asset ids or assets that contain localization data in the expected format. I18n will automatically load translations from these assets as the assets are loaded and it will also automatically unload translations if the assets get removed or unloaded at runtime.
Source:

Methods

(private) addData(data)

Adds localization data. If the locale and key for a translation already exists it will be overwritten.
Parameters:
Name Type Description
data Object The localization data. See example for the expected format of the data.
Source:
Example
this.app.i18n.addData({
  header: {
     version: 1
  },
  data: [{
     info: {
         locale: 'en-US'
     },
     messages: {
         "key": "translation",
          // The number of plural forms depends on the locale. See the manual for more information.
         "plural_key": ["one item", "more than one items"]
     }
  }, {
     info: {
         locale: 'fr-FR'
     },
     messages: {
        // ...
     }
  }]
});

(private) destroy()

Frees up memory.
Source:

(private) findAvailableLocale(desiredLocale, availableLocales) → {String}

Returns the first available locale based on the desired locale specified. First tries to find the desired locale and then tries to find an alternative locale based on the language.
Parameters:
Name Type Description
desiredLocale String The desired locale e.g. en-US.
availableLocales Object A dictionary where each key is an available locale.
Source:
Returns:
The locale found or if no locale is available returns the default en-US locale.
Type
String

(private) getPluralText(key, n, localeopt) → {String}

Returns the pluralized translation for the specified key, number n and locale. If the locale is not specified it will use the current locale.
Parameters:
Name Type Attributes Description
key String The localization key
n Nubmer The number used to determine which plural form to use. E.g. for the phrase "5 Apples" n equals 5.
locale String <optional>
The desired locale.
Source:
Returns:
The translated text. If no translations are found at all for the locale then it will return the en-US translation. If no translation exists for that key then it will return the localization key.
Type
String
Example
// manually replace {number} in the resulting translation with our number
var localized = this.app.i18n.getPluralText('{number} apples', number).replace("{number}", number);

(private) getText(key, localeopt) → {String}

Returns the translation for the specified key and locale. If the locale is not specified it will use the current locale.
Parameters:
Name Type Attributes Description
key String The localization key
locale String <optional>
The desired locale.
Source:
Returns:
The translated text. If no translations are found at all for the locale then it will return the en-US translation. If no translation exists for that key then it will return the localization key.
Type
String
Example
var localized = this.app.i18n.getText('localization-key');
var localizedFrench = this.app.i18n.getText('localization-key', 'fr-FR');

(private) removeData(data)

Removes localization data.
Parameters:
Name Type Description
data Object The localization data. The data is expected to be in the same format as pc.I18n#addData.
Source: