Options
All
  • Public
  • Public/Protected
  • All
Menu

Class DOMLocalization

The DOMLocalization class is responsible for fetching resources and formatting translations.

It implements the fallback strategy in case of errors encountered during the formatting of translations and methods for observing DOM trees with a MutationObserver.

Hierarchy

Index

Constructors

constructor

  • new DOMLocalization(resourceIds: string[], generateBundles: Function): DOMLocalization

Properties

bundles

bundles: any

generateBundles

generateBundles: Function

mutationObserver

mutationObserver: null | MutationObserver

observerConfig

observerConfig: { attributeFilter: string[]; attributes: boolean; characterData: boolean; childList: boolean; subtree: boolean }

Type declaration

  • attributeFilter: string[]
  • attributes: boolean
  • characterData: boolean
  • childList: boolean
  • subtree: boolean

pendingElements

pendingElements: Set<any>

pendingrAF

pendingrAF: any

resourceIds

resourceIds: string[]

roots

roots: Set<any>

windowElement

windowElement: null | (Window & typeof globalThis)

Methods

addResourceIds

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

Private applyTranslations

  • applyTranslations(elements: Element[], translations: Object[]): void
  • Applies translations onto elements.

    Parameters

    • elements: Element[]
    • translations: Object[]

    Returns void

connectRoot

  • connectRoot(newRoot: Element): void
  • Add newRoot to the list of roots managed by this DOMLocalization.

    Additionally, if this DOMLocalization has an observer, start observing newRoot in order to translate mutations in it.

    Parameters

    • newRoot: Element

      Root to observe.

    Returns void

disconnectRoot

  • disconnectRoot(root: Element): boolean
  • Remove root from the list of roots managed by this DOMLocalization.

    Additionally, if this DOMLocalization has an observer, stop observing root.

    Returns true if the root was the last one managed by this DOMLocalization.

    Parameters

    • root: Element

      Root to disconnect.

    Returns boolean

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

getAttributes

  • getAttributes(element: Element): { args: Object; id: string }
  • Get the data-l10n-* attributes from DOM elements.

    localization.getAttributes(
      document.querySelector('#welcome')
    );
    // -> { id: 'hello', args: { who: 'world' } }
    

    Parameters

    • element: Element

      HTML element

    Returns { args: Object; id: string }

    • args: Object
    • id: string

Private getKeysForElement

  • getKeysForElement(element: Element): Object
  • Get the data-l10n-* attributes from DOM elements as a two-element array.

    Parameters

    • element: Element

    Returns Object

Private getTranslatables

  • getTranslatables(element: Element): Element[]
  • Collects all translatable child elements of the element.

    Parameters

    • element: Element

    Returns Element[]

handleEvent

  • handleEvent(): void

onChange

  • onChange(eager?: boolean): void

Private pauseObserving

  • pauseObserving(): void

removeResourceIds

  • removeResourceIds(resourceIds: any): number

Private resumeObserving

  • resumeObserving(): void

setAttributes

  • setAttributes(element: Element, id: string, args: Object): Element
  • Set the data-l10n-id and data-l10n-args attributes on DOM elements. FluentDOM makes use of mutation observers to detect changes to data-l10n-* attributes and translate elements asynchronously. setAttributes is a convenience method which allows to translate DOM elements declaratively.

    You should always prefer to use data-l10n-id on elements (statically in HTML or dynamically via setAttributes) over manually retrieving translations with format. The use of attributes ensures that the elements can be retranslated when the user changes their language preferences.

    localization.setAttributes(
      document.querySelector('#welcome'), 'hello', { who: 'world' }
    );
    

    This will set the following attributes on the #welcome element. The MutationObserver will pick up this change and will localize the element asynchronously.

    <p id='welcome'
      data-l10n-id='hello'
      data-l10n-args='{"who": "world"}'>
    </p>
    

    Parameters

    • element: Element

      Element to set attributes on

    • id: string

      l10n-id string

    • args: Object

      KVP list of l10n arguments

    Returns Element

translateElements

  • translateElements(elements: Element[]): Promise<any>
  • Translate a list of DOM elements asynchronously using this DOMLocalization object.

    Manually trigger the translation (or re-translation) of a list of elements. Use the data-l10n-id and data-l10n-args attributes to mark up the DOM with information about which translations to use.

    Returns a Promise that gets resolved once the translation is complete.

    Parameters

    • elements: Element[]

      List of elements to be translated

    Returns Promise<any>

translateFragment

  • translateFragment(frag: any): Promise<any>
  • Translate a DOM element or fragment asynchronously using this DOMLocalization object.

    Manually trigger the translation (or re-translation) of a DOM fragment. Use the data-l10n-id and data-l10n-args attributes to mark up the DOM with information about which translations to use.

    Returns a Promise that gets resolved once the translation is complete.

    Parameters

    • frag: any

      Element or DocumentFragment to be translated

    Returns Promise<any>

Private translateMutations

  • translateMutations(mutations: any): void
  • Translate mutations detected by the MutationObserver.

    Parameters

    • mutations: any

    Returns void

translateRoots

  • translateRoots(): Promise<any>
  • Translate all roots associated with this DOMLocalization.

    Returns Promise<any>