addon-sdk/source/lib/sdk/l10n/loader.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     4 "use strict";
     6 module.metadata = {
     7   "stability": "unstable"
     8 };
    10 const { Cc, Ci } = require("chrome");
    11 const { getPreferedLocales, findClosestLocale } = require("./locale");
    12 const { readURI } = require("../net/url");
    13 const { resolve } = require("../core/promise");
    15 function parseJsonURI(uri) {
    16   return readURI(uri).
    17     then(JSON.parse).
    18     then(null, function (error) {
    19       throw Error("Failed to parse locale file:\n" + uri + "\n" + error);
    20     });
    21 }
    23 // Returns the array stored in `locales.json` manifest that list available
    24 // locales files
    25 function getAvailableLocales(rootURI) {
    26   let uri = rootURI + "locales.json";
    27   return parseJsonURI(uri).then(function (manifest) {
    28     return "locales" in manifest &&
    29            Array.isArray(manifest.locales) ?
    30            manifest.locales : [];
    31   });
    32 }
    34 // Returns URI of the best locales file to use from the XPI
    35 function getBestLocale(rootURI) {
    36   // Read localization manifest file that contains list of available languages
    37   return getAvailableLocales(rootURI).then(function (availableLocales) {
    38     // Retrieve list of prefered locales to use
    39     let preferedLocales = getPreferedLocales();
    41     // Compute the most preferable locale to use by using these two lists
    42     return findClosestLocale(availableLocales, preferedLocales);
    43   });
    44 }
    46 /**
    47  * Read localization files and returns a promise of data to put in `@l10n/data`
    48  * pseudo module, in order to allow l10n/json/core to fetch it.
    49  */
    50 exports.load = function load(rootURI) {
    51   // First, search for a locale file:
    52   return getBestLocale(rootURI).then(function (bestMatchingLocale) {
    53     // It may be null if the addon doesn't have any locale file
    54     if (!bestMatchingLocale)
    55       return resolve(null);
    57     let localeURI = rootURI + "locale/" + bestMatchingLocale + ".json";
    59     // Locale files only contains one big JSON object that is used as
    60     // an hashtable of: "key to translate" => "translated key"
    61     // TODO: We are likely to change this in order to be able to overload
    62     //       a specific key translation. For a specific package, module or line?
    63     return parseJsonURI(localeURI).then(function (json) {
    64       return {
    65         hash: json,
    66         bestMatchingLocale: bestMatchingLocale
    67       };
    68     });
    69   });
    70 }

mercurial