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

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:c09b5dddb984
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";
5
6 module.metadata = {
7 "stability": "unstable"
8 };
9
10 const { Cc, Ci } = require("chrome");
11 const { getPreferedLocales, findClosestLocale } = require("./locale");
12 const { readURI } = require("../net/url");
13 const { resolve } = require("../core/promise");
14
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 }
22
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 }
33
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();
40
41 // Compute the most preferable locale to use by using these two lists
42 return findClosestLocale(availableLocales, preferedLocales);
43 });
44 }
45
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);
56
57 let localeURI = rootURI + "locale/" + bestMatchingLocale + ".json";
58
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