Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Localization

The Localization class is a central high-level API for vanilla JavaScript use of Fluent. It combines language negotiation, FluentBundle and I/O to provide a scriptable API to format translations.

Hierarchy

Index

Constructors

constructor

  • new Localization(resourceIds?: string[], generateBundles: Function): Localization
  • Parameters

    • resourceIds: string[] = ...

      List of resource IDs

    • generateBundles: Function

      Function that returns a generator over FluentBundles

    Returns Localization

Properties

bundles

bundles: any

generateBundles

generateBundles: Function

resourceIds

resourceIds: string[]

Methods

addResourceIds

  • addResourceIds(resourceIds: any, eager?: boolean): number
  • Parameters

    • resourceIds: any
    • eager: boolean = false

    Returns number

Private formatMessages

  • formatMessages(keys: Object[]): Promise<{ attributes: Object; value: string }[]>
  • Format translations into {value, attributes} objects.

    The fallback logic is the same as in formatValues but it returns {value, attributes} objects which are suitable for the translation of DOM elements.

    docL10n.formatMessages([
      {id: 'hello', args: { who: 'Mary' }},
      {id: 'welcome'}
    ]).then(console.log);
    
    // [
    //   { value: 'Hello, Mary!', attributes: null },
    //   {
    //     value: 'Welcome!',
    //     attributes: [ { name: "title", value: 'Hello' } ]
    //   }
    // ]
    

    Returns a Promise resolving to an array of the translation strings.

    Parameters

    • keys: Object[]

    Returns Promise<{ attributes: Object; value: string }[]>

    }

formatValue

  • formatValue(id: string, args: undefined | Object): Promise<string>
  • Retrieve the translation corresponding to the id identifier.

    If passed, args is a simple hash object with a list of variables that will be interpolated in the value of the translation.

    docL10n.formatValue(
      'hello', { who: 'world' }
    ).then(console.log);
    
    // 'Hello, world!'
    

    Returns a Promise resolving to the translation string.

    Use this sparingly for one-off messages which don't need to be retranslated when the user changes their language preferences, e.g. in notifications.

    Parameters

    • id: string

      Identifier of the translation to format

    • args: undefined | Object

    Returns Promise<string>

formatValues

  • formatValues(keys: Object[]): Promise<string[]>
  • Retrieve translations corresponding to the passed keys.

    A generalized version of DOMLocalization.formatValue. Keys must be {id, args} objects.

    docL10n.formatValues([
      {id: 'hello', args: { who: 'Mary' }},
      {id: 'hello', args: { who: 'John' }},
      {id: 'welcome'}
    ]).then(console.log);
    
    // ['Hello, Mary!', 'Hello, John!', 'Welcome!']
    

    Returns a Promise resolving to an array of the translation strings.

    Parameters

    • keys: Object[]

    Returns Promise<string[]>

Private formatWithFallback

  • formatWithFallback(keys: Object[], method: Function): Promise<(string | Object)[]>
  • Format translations and handle fallback if needed.

    Format translations for keys from FluentBundle instances on this DOMLocalization. In case of errors, fetch the next context in the fallback chain.

    Parameters

    • keys: Object[]

      Translation keys to format.

    • method: Function

      Formatting function.

    Returns Promise<(string | Object)[]>

handleEvent

  • handleEvent(): void

onChange

  • onChange(eager?: boolean): void
  • This method should be called when there's a reason to believe that language negotiation or available resources changed.

    Parameters

    • eager: boolean = false

    Returns void

removeResourceIds

  • removeResourceIds(resourceIds: any): number