1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 * An API which allows Marionette to handle localized content.
8 * The localization (https://mzl.la/2eUMjyF) of UI elements in Gecko
9 * based applications is done via entities and properties. For static
10 * values entities are used, which are located in .dtd files. Whereby for
11 * dynamically updated content the values come from .property files. Both
12 * types of elements can be identifed via a unique id, and the translated
18 ChromeUtils.defineESModuleGetters(lazy, {
19 error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
22 ChromeUtils.defineLazyGetter(lazy, "domParser", () => {
23 const parser = new DOMParser();
24 parser.forceEnableDTD();
29 export const l10n = {};
32 * Retrieve the localized string for the specified entity id.
35 * localizeEntity(["chrome://branding/locale/brand.dtd"], "brandShortName")
37 * @param {Array.<string>} urls
40 * The ID of the entity to retrieve the localized string for.
43 * The localized string for the requested entity.
45 l10n.localizeEntity = function (urls, id) {
46 // Build a string which contains all possible entity locations
48 urls.forEach((url, index) => {
49 locations.push(`<!ENTITY % dtd_${index} SYSTEM "${url}">%dtd_${index};`);
52 // Use the DOM parser to resolve the entity and extract its real value
53 let header = `<?xml version="1.0"?><!DOCTYPE elem [${locations.join("")}]>`;
54 let elem = `<elem id="elementID">&${id};</elem>`;
55 let doc = lazy.domParser.parseFromString(header + elem, "text/xml");
56 let element = doc.querySelector("elem[id='elementID']");
58 if (element === null) {
59 throw new lazy.error.NoSuchElementError(
60 `Entity with id='${id}' hasn't been found`
64 return element.textContent;
68 * Retrieve the localized string for the specified property id.
73 * ["chrome://global/locale/findbar.properties"], "FastFind");
75 * @param {Array.<string>} urls
76 * Array of .properties URLs.
78 * The ID of the property to retrieve the localized string for.
81 * The localized string for the requested property.
83 l10n.localizeProperty = function (urls, id) {
86 for (let url of urls) {
87 let bundle = Services.strings.createBundle(url);
89 property = bundle.GetStringFromName(id);
94 if (property === null) {
95 throw new lazy.error.NoSuchElementError(
96 `Property with ID '${id}' hasn't been found`