michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "unstable" michael@0: }; michael@0: michael@0: const { Cc, Ci } = require("chrome"); michael@0: const { getPreferedLocales, findClosestLocale } = require("./locale"); michael@0: const { readURI } = require("../net/url"); michael@0: const { resolve } = require("../core/promise"); michael@0: michael@0: function parseJsonURI(uri) { michael@0: return readURI(uri). michael@0: then(JSON.parse). michael@0: then(null, function (error) { michael@0: throw Error("Failed to parse locale file:\n" + uri + "\n" + error); michael@0: }); michael@0: } michael@0: michael@0: // Returns the array stored in `locales.json` manifest that list available michael@0: // locales files michael@0: function getAvailableLocales(rootURI) { michael@0: let uri = rootURI + "locales.json"; michael@0: return parseJsonURI(uri).then(function (manifest) { michael@0: return "locales" in manifest && michael@0: Array.isArray(manifest.locales) ? michael@0: manifest.locales : []; michael@0: }); michael@0: } michael@0: michael@0: // Returns URI of the best locales file to use from the XPI michael@0: function getBestLocale(rootURI) { michael@0: // Read localization manifest file that contains list of available languages michael@0: return getAvailableLocales(rootURI).then(function (availableLocales) { michael@0: // Retrieve list of prefered locales to use michael@0: let preferedLocales = getPreferedLocales(); michael@0: michael@0: // Compute the most preferable locale to use by using these two lists michael@0: return findClosestLocale(availableLocales, preferedLocales); michael@0: }); michael@0: } michael@0: michael@0: /** michael@0: * Read localization files and returns a promise of data to put in `@l10n/data` michael@0: * pseudo module, in order to allow l10n/json/core to fetch it. michael@0: */ michael@0: exports.load = function load(rootURI) { michael@0: // First, search for a locale file: michael@0: return getBestLocale(rootURI).then(function (bestMatchingLocale) { michael@0: // It may be null if the addon doesn't have any locale file michael@0: if (!bestMatchingLocale) michael@0: return resolve(null); michael@0: michael@0: let localeURI = rootURI + "locale/" + bestMatchingLocale + ".json"; michael@0: michael@0: // Locale files only contains one big JSON object that is used as michael@0: // an hashtable of: "key to translate" => "translated key" michael@0: // TODO: We are likely to change this in order to be able to overload michael@0: // a specific key translation. For a specific package, module or line? michael@0: return parseJsonURI(localeURI).then(function (json) { michael@0: return { michael@0: hash: json, michael@0: bestMatchingLocale: bestMatchingLocale michael@0: }; michael@0: }); michael@0: }); michael@0: }