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.

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

mercurial