Options
All
  • Public
  • Public/Protected
  • All
Menu

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

Hierarchy

Index

Constructors

constructor

  • new Locale(locale?: string): Locale

Properties

Protected Optional locale

locale: string

Accessors

Private appClassApplication

Protected config

Methods

Protected app

Protected container

get

  • get(phrase: string, __namedParameters?: { fallback?: string; interp?: {}; plural?: number }): string
  • 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?: {}; plural?: number } = {}
      • Optional fallback?: string
      • Optional interp?: {}
        • [key: string]: any
      • Optional plural?: number

    Returns string

getBoundMethod

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

    Parameters

    • methodName: string

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

    function

      • (...args: any[]): any
      • Parameters

        • Rest ...args: any[]

        Returns any

getDefaultLocale

  • getDefaultLocale(): string
  • Get the default locale that should be assigned if none is specified in the session.

    Returns string

    string

helper

Protected load

  • load(locale: string): string | {}
  • Attempt to load the given locale string, merging in the appropriate common locale values.

    Parameters

    • locale: string

    Returns string | {}

Protected make

  • make<T>(target: any, ...parameters: any[]): T

setLocale

  • setLocale(locale: string): void
Extollo Logo

extollo (v. latin) - to lift up, to elevate

Extollo is a free & libre application framework in TypeScript.