A helper class which assists with localization and string translation

Param: serverLanguage

The default language configuration setting for the server

Properties

lang: string

The target language for localization

defaultModule: string

The package authorized to provide default language configurations

translations: Object

The translation dictionary for the target language

_fallback: Object

Fallback translations if the target keys are not found

#formatters: Record<string, ListFormat> = {}

Cached store of Intl.ListFormat instances.

Methods

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

    Returns Promise<void>

    A Promise which resolves once languages are initialized

  • Set a language as the active translation source for the session

    Parameters

    • lang: string

      A language string in CONFIG.supportedLanguages

    Returns Promise<void>

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

  • Return whether a certain string has a known translation defined.

    Parameters

    • stringId: string

      The string key being translated

    • Optional fallback: boolean = true

      Allow fallback translations to count?

    Returns boolean

  • 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

    Parameters

    • stringId: string

      The string ID to translate

    Returns string

    The translated string

    Example: Localizing a simple string in JavaScript

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

    Example: Localizing a simple string in Handlebars

    {{localize "MYMODULE.MYSTRING"}} <!-- Hello, this is my module! -->
    
  • 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.

    Parameters

    • stringId: string

      The string ID to translate

    • data: object = {}

      Provided input data

    Returns string

    The translated and formatted string

    Example: 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!

    Example: Localizing a formatted string in Handlebars

    {{localize "MYMODULE.GREETING" name="Andrew"}} <!-- Hello, this is my module! -->
    
  • Retrieve list formatter configured to the world's language setting.

    Parameters

    • Optional options: {
          style: ListFormatStyle;
          type: ListFormatType;
      } = {}
      • style: ListFormatStyle

        The list formatter style, either "long", "short", or "narrow".

      • type: ListFormatType

        The list formatter type, either "conjunction", "disjunction", or "unit".

    Returns ListFormat

  • Sort an array of objects by a given key in a localization-aware manner.

    Parameters

    • objects: object[]

      The objects to sort, this array will be mutated.

    • key: string

      The key to sort the objects by. This can be provided in dot-notation.

    Returns object[]

  • Private

    Discover the available supported languages from the set of packages which are provided

    Returns object

    The resulting configuration of supported languages

  • Private

    Prepare the dictionary of translation strings for the requested language

    Parameters

    • lang: string

      The language for which to load translations

    Returns Promise<object>

    The retrieved translations object

  • Private

    Reduce the languages array provided by a package to an array of file paths of translations to load

    Parameters

    • pkg: object

      The package data

    • lang: string

      The target language to filter on

    Returns string[]

    An array of translation file paths

  • Private

    Load a single translation file and return its contents as processed JSON

    Parameters

    • src: string

      The translation file path to load

    Returns Promise<object>

    The loaded translation dictionary

  • Perform one-time localization of the fields in a DataModel schema, translating their label and hint properties.

    Parameters

    • model: any

      The DataModel class to localize

    • options: {
          prefixes: string[];
          prefixPath: string;
      } = {}

      Options which configure how localization is performed

      • prefixes: string[]

        An array of localization key prefixes to use. If not specified, prefixes are learned from the DataModel.LOCALIZATION_PREFIXES static property.

      • prefixPath: string

        A localization path prefix used to prefix all field names within this model. This is generally not required.

    Returns void

    Example

    JavaScript class definition and localization call.

    class MyDataModel extends foundry.abstract.DataModel {
    static defineSchema() {
    return {
    foo: new foundry.data.fields.StringField(),
    bar: new foundry.data.fields.NumberField()
    };
    }
    static LOCALIZATION_PREFIXES = ["MYMODULE.MYDATAMODEL"];
    }

    Hooks.on("i18nInit", () => {
    Localization.localizeDataModel(MyDataModel);
    });

    JSON localization file

    {
    "MYMODULE": {
    "MYDATAMODEL": {
    "FIELDS" : {
    "foo": {
    "label": "Foo",
    "hint": "Instructions for foo"
    },
    "bar": {
    "label": "Bar",
    "hint": "Instructions for bar"
    }
    }
    }
    }
    }
  • Perform one-time localization of data model definitions which localizes their label and hint properties.

    Returns void

  • Localize the "label" and "hint" properties for all fields in a data schema.

    Parameters

    • schema: SchemaField
    • prefixes: string[] = []
    • Optional options: {
          prefixPath: string;
      } = {}
      • prefixPath: string

    Returns void