Localization

Localization

A helper class which assists with localization and string translation

Constructor

new Localization(serverLanguage)

Parameters:
Name Type Description
serverLanguage string

The default language configuration setting for the server

Members

_fallback :Object

Fallback translations if the target keys are not found

Type:
  • Object

defaultModule :string

The package authorized to provide default language configurations

Type:
  • string

lang :string

The target language for localization

Type:
  • string

translations :Object

The translation dictionary for the target language

Type:
  • Object

Methods

format(stringId, data) → {string}

Localize a string including variable formatting for input arguments. Provide a string ID which defines the localized template. Variables can be included in the template enclosed in braces and will be substituted using those named keys.

Examples

Localizing a formatted string in JavaScript

{
  "MYMODULE.GREETING": "Hello {name}, this is my module!"
}
game.i18n.format("MYMODULE.GREETING" {name: "Andrew"}); // Hello Andrew, this is my module!

Localizing a formatted string in Handlebars

{{localize "MYMODULE.GREETING" name="Andrew"}} <!-- Hello, this is my module! -->
Parameters:
Name Type Description
stringId string

The string ID to translate

data object

Provided input data

Returns:

The translated and formatted string

Type
string

has(stringId, fallbackopt) → {boolean}

Return whether a certain string has a known translation defined.

Parameters:
Name Type Attributes Default Description
stringId string

The string key being translated

fallback boolean <optional>
true

Allow fallback translations to count?

Returns:
Type
boolean

(async) initialize() → {Promise.<void>}

Initialize the Localization module Discover available language translations and apply the current language setting

Returns:

A Promise which resolves once languages are initialized

Type
Promise.<void>

localize(stringId) → {string}

Localize a string by drawing a translation from the available translations dictionary, if available If a translation is not available, the original string is returned

Examples

Localizing a simple string in JavaScript

{
  "MYMODULE.MYSTRING": "Hello, this is my module!"
}
game.i18n.localize("MYMODULE.MYSTRING"); // Hello, this is my module!

Localizing a simple string in Handlebars

{{localize "MYMODULE.MYSTRING"}} <!-- Hello, this is my module! -->
Parameters:
Name Type Description
stringId string

The string ID to translate

Returns:

The translated string

Type
string

(async) setLanguage(lang) → {Promise.<void>}

Set a language as the active translation source for the session

Parameters:
Name Type Description
lang string

A language string in CONFIG.supportedLanguages

Returns:

A Promise which resolves once the translations for the requested language are ready

Type
Promise.<void>