A helper class which assists with localization and string translation

The default language configuration setting for the server

Properties

_fallback: Object

Fallback translations if the target keys are not found

defaultModule: string

The package authorized to provide default language configurations

lang: string

The target language for localization

translations: Object

The translation dictionary for the target language

Methods

  • 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

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

    Parameters

    • Optionaloptions: { style?: ListFormatStyle; type?: ListFormatType } = {}
      • Optionalstyle?: ListFormatStyle

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

      • Optionaltype?: ListFormatType

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

    Returns ListFormat

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

    Parameters

    • stringId: string

      The string key being translated

    • Optionalfallback: boolean = true

      Allow fallback translations to count?

    Returns boolean

  • 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

  • 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

    {
    "MYMODULE.MYSTRING": "Hello, this is my module!"
    }
    game.i18n.localize("MYMODULE.MYSTRING"); // Hello, this is my module!
    {{localize "MYMODULE.MYSTRING"}} <!-- Hello, this is my module! -->
    
  • 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

  • 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[]

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

    Parameters

    • model: typeof DataModel

      The DataModel class to localize

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

      Options which configure how localization is performed

      • Optionalprefixes?: string[]

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

      • OptionalprefixPath?: string

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

    Returns void

    DataModel.LOCALIZATION_PREFIXES for an example of the class definition and localization file structure.

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

    Parameters

    • schema: SchemaField
    • prefixes: string[] = []
    • Optionaloptions: { prefixPath?: string; seenFields?: Set<DataField> } = {}

    Returns void