DOMLocalization
The DOMLocalization class is an extension of the Localization class and is responsible for fetching resources and formatting translations for DOM elements.
Functionality
Includes all the functionality from
Localization
constructor(resourceIds, generateBundles)
Creates a new
DOMLocalizationobject given a list ofresourceIDsand agenerateBundlesfunction that returns a generator overFluentBundles.The
resourceIdsare the paths to the fluent files that are used to generate bundles.The
generateBundlesfunction's generator behavior acts as a fallbacking strategy for the availability of fluent resources. Fallbacking allows for translations from a different language to be used if the translation is not available in the desired language. For example, if the Spanish translation is missing, English text could be displayed instead.The
generateBundlesfunction's generator behavior acts as a fallbacking strategy for the availability of fluent resources.When localizing content in the ideal scenario, the
generateBundlesfunction will only ever have to produce one bundle. This would mean that the first bundle had translations for every requested localization.If the current bundle in unable to produce a translation, then the generator's next bundle will be retrieved in an attempt to find a translation, and so on.
setAttributes(element, id, args)
Set the
data-l10n-idanddata-l10n-argsattributes on a DOMelement.DOMLocalizationmakes use of mutation observers to detect change todata-l10n-*attributes and translate elements asynchronously.setAttributesis a convenience method which allows to translate DOM elements declaratively.You should always prefer to use
data-l10n-idon elements (statically in HTML or dynamically viasetAttributes) over manually retrieving translations withformat. The use of attributes ensures that the elements can be retranslated when the user changes their language preferences.localization.setAttributes( document.querySelector('#welcome'), 'hello', { name: 'world' } );This will set the following attributes on the
#welcomeelement. The MutationObserver will pick up this change and will localize the element asynchronously.<p id='welcome' data-l10n-id='hello' data-l10n-args='{"name": "world"}'> </p>
getAttributes(element)
Get the
data-l10n-*attributes from a DOMelement.localization.setAttributes( document.querySelector('#welcome'), 'hello', { name: 'world' } ) localization.getAttributes( document.querySelector('#welcome') ); // -> { id: 'hello', args: { name: 'world' } }
connectRoot(root)
Add new
rootto the list of roots managed by thisDOMLocalization.Additionally, if this
DOMLocalizationhas an observer, start observing the newrootin order to translate mutations in it.The new
rootmay not overlap with an existing root.
disconnectRoot(root)
Remove
rootfrom the list of roots managed by thisDOMLocalization.Additionally, if this
DOMLocalizationhas an observer, stop observingroot.Returns
trueif the root was the last one managed by thisDOMLocalization.
translateRoots()
Translate all roots associated with this
DOMLocalization.
translateFragment(fragment)
Translate a DOM element or fragment asynchronously using this
DOMLocalizationobject.Manually trigger the translation (or re-translation) of a DOM fragment. Use the
data-l10n-idanddata-l10n-argsattributes to mark up the DOM with information about which translations to use.Returns a
Promisethat gets resolved once the translation is complete.
translateElements(elements)
Translate a list of DOM elements asynchronously using this
DOMLocalizationobject.Manually trigger the translation (or re-translation) of a list of elements. Use the
data-l10n-idanddata-l10n-argsattributes to mark up the DOM with information about which translations to use.Returns a
Promisethat gets resolved once the translation is complete.