Options
All
  • Public
  • Public/Protected
  • All
Menu

A helper class which assists with localization and string translation

param serverLanguage

The default language configuration setting for the server

Hierarchy

  • Localization

Index

Constructors

Properties

lang: string

The target language for localization

defaultModule: string

The package authorized to provide default language configurations

translations: any

The translation dictionary for the target language

_fallback: any

Fallback translations if the target keys are not found

#formatters: any = {}

Cached store of Intl.ListFormat instances.

Methods

  • initialize(): Promise<void>
  • 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

  • setLanguage(lang: string): Promise<void>
  • 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

  • has(stringId: string, fallback?: boolean): boolean
  • Return whether a certain string has a known translation defined.

    Parameters

    • stringId: string

      The string key being translated

    • fallback: boolean = true

    Returns boolean

  • localize(stringId: string): 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

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

    Parameters

    • stringId: string

      The string ID to translate

    Returns string

    The translated string

  • format(stringId: string, data?: any): 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.

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

    Parameters

    • stringId: string

      The string ID to translate

    • data: any = {}

      Provided input data

    Returns string

    The translated and formatted string

  • getListFormatter([options]?: { style: ListFormatStyle; type: ListFormatType }): ListFormat
  • Retrieve list formatter configured to the world's language setting.

    see

    Intl.ListFormat

    Parameters

    • [options]: { style: ListFormatStyle; type: ListFormatType } = {}
      • style: ListFormatStyle
      • type: ListFormatType

    Returns ListFormat

  • sortObjects(objects: any[], key: string): any[]
  • Sort an array of objects by a given key in a localization-aware manner.

    Parameters

    • objects: any[]

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

  • _discoverSupportedLanguages(): any
  • Discover the available supported languages from the set of packages which are provided

    Returns any

    The resulting configuration of supported languages

  • _getTranslations(lang: string): Promise<any>
  • Prepare the dictionary of translation strings for the requested language

    Parameters

    • lang: string

      The language for which to load translations

    Returns Promise<any>

    The retrieved translations object

  • _filterLanguagePaths(pkg: any, lang: string): string[]
  • Reduce the languages array provided by a package to an array of file paths of translations to load

    Parameters

    • pkg: any

      The package data

    • lang: string

      The target language to filter on

    Returns string[]

    An array of translation file paths

  • _loadTranslationFile(src: string): Promise<any>
  • Load a single translation file and return its contents as processed JSON

    Parameters

    • src: string

      The translation file path to load

    Returns Promise<any>

    The loaded translation dictionary