1 /* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/.
8 * An object used to carry localization information from and to an
12 * id - identifier of a message used to localize the element.
13 * args - an optional map of arguments used to format the message.
14 * The argument will be converted to/from JSON, so can
15 * handle any value that JSON can, but in practice, the API
16 * will only use a string or a number for now.
19 required DOMString id;
24 * DocumentL10n is a public interface for handling DOM localization.
26 * In it's current form it exposes the DOMLocalization API which
27 * allows for localization-specific DOM operations on a defined
28 * localization context, and retrival of formatted localization
29 * messages out of that context.
31 * The context is created when `<link rel="localization"/>` elements
32 * are added to the document, and is removed in case all links
33 * of that type are removed from it.
36 interface DocumentL10n {
38 * Formats a value of a localization message with a given id.
39 * An optional dictionary of arguments can be passed to inform
40 * the message formatting logic.
43 * let value = await document.l10n.formatValue("unread-emails", {count: 5});
44 * assert.equal(value, "You have 5 unread emails");
46 [NewObject] Promise<DOMString> formatValue(DOMString aId, optional object aArgs);
49 * Formats values of a list of messages with given ids.
52 * let values = await document.l10n.formatValues([
53 * {id: "hello-world"},
54 * {id: "unread-emails", args: {count: 5}
56 * assert.deepEqual(values, [
58 * "You have 5 unread emails"
61 [NewObject] Promise<sequence<DOMString>> formatValues(sequence<L10nKey> aKeys);
64 * Formats values and attributes of a list of messages with given ids.
67 * let values = await document.l10n.formatMessages([
68 * {id: "hello-world"},
69 * {id: "unread-emails", args: {count: 5}
71 * assert.deepEqual(values, [
73 * value: "Hello World",
77 * value: "You have 5 unread emails",
79 * tooltip: "Click to select them all"
84 [NewObject] Promise<sequence<DOMString>> formatMessages(sequence<L10nKey> aKeys);
87 * A helper function which allows the user to set localization-specific attributes
89 * This method lives on `document.l10n` for compatibility reasons with the
90 * JS FluentDOM implementation. We may consider moving it onto Element.
93 * document.l10n.setAttributes(h1, "key1", { emailCount: 5 });
95 * <h1 data-l10n-id="key1" data-l10n-args="{\"emailCount\": 5}"/>
97 [Throws] void setAttributes(Element aElement, DOMString aId, optional object aArgs);
100 * A helper function which allows the user to retrieve a set of localization-specific
101 * attributes from an element.
102 * This method lives on `document.l10n` for compatibility reasons with the
103 * JS FluentDOM implementation. We may consider moving it onto Element.
106 * let l10nAttrs = document.l10n.getAttributes(h1);
107 * assert.deepEqual(l10nAttrs, {id: "key1", args: { emailCount: 5});
109 [Throws] L10nKey getAttributes(Element aElement);
112 * Triggers translation of a subtree rooted at aNode.
114 * The method finds all translatable descendants of the argument and
117 * This method is mainly useful to trigger translation not covered by the
118 * DOMLocalization's MutationObserver - for example before it gets attached
122 * await document.l10n.translatFragment(frag);
123 * parent.appendChild(frag);
125 [NewObject] Promise<void> translateFragment(Node aNode);
128 * Triggers translation of a list of Elements using the localization context.
130 * The method only translates the elements directly passed to the method,
131 * not any descendant nodes.
133 * This method is mainly useful to trigger translation not covered by the
134 * DOMLocalization's MutationObserver - for example before it gets attached
138 * await document.l10n.translateElements([elem1, elem2]);
139 * parent.appendChild(elem1);
140 * alert(elem2.textContent);
142 [NewObject] Promise<void> translateElements(sequence<Element> aElements);
145 * A promise which gets resolved when the initial DOM localization resources
146 * fetching is complete and the initial translation of the DOM is finished.
148 readonly attribute Promise<any> ready;