tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

l10n.sys.mjs (1666B)


      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/. */
      4 
      5 /**
      6 * An API which allows Marionette to handle localized content.
      7 *
      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 identified via a unique id, and the translated
     13 * content retrieved.
     14 */
     15 
     16 const lazy = {};
     17 
     18 ChromeUtils.defineESModuleGetters(lazy, {
     19  error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
     20 });
     21 
     22 /** @namespace */
     23 export const l10n = {};
     24 
     25 /**
     26 * Retrieve the localized string for the specified property id.
     27 *
     28 * Example:
     29 *
     30 *     localizeProperty(
     31 *         ["chrome://global/locale/findbar.properties"], "FastFind");
     32 *
     33 * @param {Array.<string>} urls
     34 *     Array of .properties URLs.
     35 * @param {string} id
     36 *     The ID of the property to retrieve the localized string for.
     37 *
     38 * @returns {string}
     39 *     The localized string for the requested property.
     40 */
     41 l10n.localizeProperty = function (urls, id) {
     42  let property = null;
     43 
     44  for (let url of urls) {
     45    let bundle = Services.strings.createBundle(url);
     46    try {
     47      property = bundle.GetStringFromName(id);
     48      break;
     49    } catch (e) {}
     50  }
     51 
     52  if (property === null) {
     53    throw new lazy.error.NoSuchElementError(
     54      `Property with ID '${id}' hasn't been found`
     55    );
     56  }
     57 
     58  return property;
     59 };