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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/l10n/loader.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,70 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +module.metadata = {
    1.10 +  "stability": "unstable"
    1.11 +};
    1.12 +
    1.13 +const { Cc, Ci } = require("chrome");
    1.14 +const { getPreferedLocales, findClosestLocale } = require("./locale");
    1.15 +const { readURI } = require("../net/url");
    1.16 +const { resolve } = require("../core/promise");
    1.17 +
    1.18 +function parseJsonURI(uri) {
    1.19 +  return readURI(uri).
    1.20 +    then(JSON.parse).
    1.21 +    then(null, function (error) {
    1.22 +      throw Error("Failed to parse locale file:\n" + uri + "\n" + error);
    1.23 +    });
    1.24 +}
    1.25 +
    1.26 +// Returns the array stored in `locales.json` manifest that list available
    1.27 +// locales files
    1.28 +function getAvailableLocales(rootURI) {
    1.29 +  let uri = rootURI + "locales.json";
    1.30 +  return parseJsonURI(uri).then(function (manifest) {
    1.31 +    return "locales" in manifest &&
    1.32 +           Array.isArray(manifest.locales) ?
    1.33 +           manifest.locales : [];
    1.34 +  });
    1.35 +}
    1.36 +
    1.37 +// Returns URI of the best locales file to use from the XPI
    1.38 +function getBestLocale(rootURI) {
    1.39 +  // Read localization manifest file that contains list of available languages
    1.40 +  return getAvailableLocales(rootURI).then(function (availableLocales) {
    1.41 +    // Retrieve list of prefered locales to use
    1.42 +    let preferedLocales = getPreferedLocales();
    1.43 +
    1.44 +    // Compute the most preferable locale to use by using these two lists
    1.45 +    return findClosestLocale(availableLocales, preferedLocales);
    1.46 +  });
    1.47 +}
    1.48 +
    1.49 +/**
    1.50 + * Read localization files and returns a promise of data to put in `@l10n/data`
    1.51 + * pseudo module, in order to allow l10n/json/core to fetch it.
    1.52 + */
    1.53 +exports.load = function load(rootURI) {
    1.54 +  // First, search for a locale file:
    1.55 +  return getBestLocale(rootURI).then(function (bestMatchingLocale) {
    1.56 +    // It may be null if the addon doesn't have any locale file
    1.57 +    if (!bestMatchingLocale)
    1.58 +      return resolve(null);
    1.59 +
    1.60 +    let localeURI = rootURI + "locale/" + bestMatchingLocale + ".json";
    1.61 +
    1.62 +    // Locale files only contains one big JSON object that is used as
    1.63 +    // an hashtable of: "key to translate" => "translated key"
    1.64 +    // TODO: We are likely to change this in order to be able to overload
    1.65 +    //       a specific key translation. For a specific package, module or line?
    1.66 +    return parseJsonURI(localeURI).then(function (json) {
    1.67 +      return {
    1.68 +        hash: json,
    1.69 +        bestMatchingLocale: bestMatchingLocale
    1.70 +      };
    1.71 +    });
    1.72 +  });
    1.73 +}

mercurial