Request-level service that provides localization of phrases based on config files.

Hierarchy

Constructors

  • Parameters

    • Optional locale: string

      The preferred locale. This corresponds to the config prefix.

      Example

      en_US means "lang:en_US" config scope
      

      Example

      es_MX means "lang:es_MX" config scope
      

    Returns Locale

Properties

locale?: string

The preferred locale. This corresponds to the config prefix.

Example

en_US means "lang:en_US" config scope

Example

es_MX means "lang:es_MX" config scope

Accessors

Methods

  • Resolve the given phrase to the locale of this service. This is where all the magic happens.

    This method tries to load the given phrase from the config using load() then localizes it using the specified parameters.

    Example

    The pluralization can be specified using the plural parameter. If an explicit pluralization is specified in the phrase config, that will be used. Otherwise, the pluralize library is used to generate one automatically.

    // Example phrase config:
    {
    apple: 'Apple',
    bunch_of_bananas: {
    one: 'Bunch of Bananas',
    many: 'Bunches of Bananas',
    }
    }

    // Example usage:
    locale.get('apple', { plural: 3 }) // => 'Apples'
    locale.get('apple') // => 'Apple'
    locale.get('bunch_of_bananas', { plural: 2 }) // => 'Bunches of Bananas'
    locale.get('bunch_of_bananas', { plural: 1}) // => 'Bunch of Bananas'

    Example

    If a translation cannot be found, and a fallback is specified, the fallback will be returned. Otherwise, the value of phrase is returned instead.

    locale.get('nonexistent_phrase', { fallback: 'Hello, world!' }) // => 'Hello, world!'
    

    Example

    Values can be interpolated into phrases using the interp parameter. For example, if there is a phrase my_phrase: 'Hello, :name:!, the value of :name: can be replaced like so:

    locale.get('my_phrase', {interp: {name: 'John Doe'}}) // => 'Hello, John Doe!'
    

    Example

    If a phrase cannot be found in the specific locale config, the service will try to load it from the equivalent common locale config. For example, if this.locale is es_MX:

    // Example "lang:common:dashboard" config:
    {
    title: "MyDash2.0",
    header: "Welcome to the dashboard!",
    }

    // Example "lang:es_MX:dashboard" config:
    {
    header: "¡Bienvenido al panel de control!",
    }

    // Example usage:
    locale.get('dashboard.title') // => 'MyDash2.0'
    locale.get('dashboard.header') // => '¡Bienvenido al panel de control!'

    Parameters

    • phrase: string
    • __namedParameters: {
          fallback?: string;
          interp?: {
              [key: string]: any;
          };
          plural?: number;
      } = {}
      • Optional fallback?: string
      • Optional interp?: {
            [key: string]: any;
        }
        • [key: string]: any
      • Optional plural?: number

    Returns string

  • Get the method with the given name from this class, bound to this class.

    Returns

    function

    Parameters

    • methodName: string

    Returns ((...args: any[]) => any)

      • (...args: any[]): any
      • Get the method with the given name from this class, bound to this class.

        Returns

        function

        Parameters

        • Rest ...args: any[]

        Returns any

  • Get the default locale that should be assigned if none is specified in the session.

    Returns

    string

    Returns string

  • Protected

    Attempt to load the given locale string, merging in the appropriate common locale values.

    Parameters

    • locale: string

    Returns string | {
        [key: string]: string;
    }

  • Call the make() method on the global container.

    Type Parameters

    • T

    Parameters

    • target: any
    • Rest ...parameters: any[]

    Returns T

  • Set the preferred locale for lookups by this service.

    Parameters

    • locale: string

    Returns void