{{ non-standard_header() }}
The setAttributes
method may be used to set the data-l10n-id
and data-l10n-args
attributes on DOM elements.
The L10n API makes use of mutation observers to detect changes to data-l10n-*
attributes and translate the affected elements asynchronously. The L10n.setAttributes
method is a convenience method which allows to translate DOM elements declaratively. Under the hood, L10n's mutation observer uses the same semantics as the {{domxref("L10n.get")}} method.
Syntax
navigator.mozL10n.setAttributes(element, identifier[, data]);
Parameters
-
element
- The element to be translated.
-
identifier
- The string identifier of the translation to be used for translation.
-
data
- An object with variables to be interpolated into the translation. All members' values must be strings or numbers.
Example
- dialer.en-US.properties:
-
from-contact = Missed call from \{{ contact }}
- dialer.js:
-
var banner = document.getElementById('banner'); navigator.mozL10n.setAttributes(banner, 'from-contact', {contact: primaryInfo.toString()});
L10n.setAttributes
is a convenience method which uses the standard Element.setAttribute API. Therefore, the above is equivalent to:var banner = document.getElementById('banner'); banner.setAttribute('data-l10n-id', 'from-contact'); banner.setAttribtue('data-l10n-args', JSON.stringify({contact: primaryInfo.toString()}));
Specification
Not part of any specification.
See also
- {{domxref("L10n")}}
- {{domxref("L10n.get")}}
- Guides
- Properties
- {{ domxref("L10n.language.code") }}
- {{ domxref("L10n.language.direction") }}
- {{ domxref("L10n.readyState") }}
- Methods
- {{ domxref("L10n.get") }}
- {{ domxref("L10n.ready") }}
- {{ domxref("L10n.once") }}