aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.12/lib/sdk/l10n/loader.js
diff options
context:
space:
mode:
authorGravatar Rogan Creswick <creswick@gmail.com>2013-01-02 15:31:43 -0800
committerGravatar Rogan Creswick <creswick@gmail.com>2013-01-02 15:31:43 -0800
commit91c8d16838b39c7726f6c0a0fb333f5ba38d2494 (patch)
tree136df17525e5e62f3ddc5b7a6dc71ffd31e2aa42 /tools/addon-sdk-1.12/lib/sdk/l10n/loader.js
parentf7b228c479bac8828e521a2826d7f57f150da877 (diff)
updated addon-sdk to 1.12
Diffstat (limited to 'tools/addon-sdk-1.12/lib/sdk/l10n/loader.js')
-rw-r--r--tools/addon-sdk-1.12/lib/sdk/l10n/loader.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/tools/addon-sdk-1.12/lib/sdk/l10n/loader.js b/tools/addon-sdk-1.12/lib/sdk/l10n/loader.js
new file mode 100644
index 0000000..9af6c5c
--- /dev/null
+++ b/tools/addon-sdk-1.12/lib/sdk/l10n/loader.js
@@ -0,0 +1,71 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+module.metadata = {
+ "stability": "unstable"
+};
+
+const { Cc, Ci } = require("chrome");
+const { getPreferedLocales, findClosestLocale } = require("./locale");
+const { readURI } = require("../net/url");
+const { resolve } = require("../core/promise");
+
+function parseJsonURI(uri) {
+ return readURI(uri).
+ then(JSON.parse).
+ then(null, function (error) {
+ throw Error("Failed to parse locale file:\n" + uri + "\n" + error);
+ });
+}
+
+// Returns the array stored in `locales.json` manifest that list available
+// locales files
+function getAvailableLocales(rootURI) {
+ let uri = rootURI + "locales.json";
+ return parseJsonURI(uri).then(function (manifest) {
+ return "locales" in manifest &&
+ Array.isArray(manifest.locales) ?
+ manifest.locales : [];
+ });
+}
+
+// Returns URI of the best locales file to use from the XPI
+function getBestLocale(rootURI) {
+ // Read localization manifest file that contains list of available languages
+ return getAvailableLocales(rootURI).then(function (availableLocales) {
+ // Retrieve list of prefered locales to use
+ let preferedLocales = getPreferedLocales();
+
+ // Compute the most preferable locale to use by using these two lists
+ return findClosestLocale(availableLocales, preferedLocales);
+ });
+}
+
+/**
+ * Read localization files and returns a promise of data to put in `@l10n/data`
+ * pseudo module, in order to allow l10n/core to fetch it.
+ */
+exports.load = function load(rootURI) {
+ // First, search for a locale file:
+ return getBestLocale(rootURI).then(function (bestMatchingLocale) {
+ // It may be null if the addon doesn't have any locale file
+ if (!bestMatchingLocale)
+ return resolve(null);
+
+ let localeURI = rootURI + "locale/" + bestMatchingLocale + ".json";
+
+ // Locale files only contains one big JSON object that is used as
+ // an hashtable of: "key to translate" => "translated key"
+ // TODO: We are likely to change this in order to be able to overload
+ // a specific key translation. For a specific package, module or line?
+ return parseJsonURI(localeURI).then(function (json) {
+ return {
+ hash: json,
+ bestMatchingLocale: bestMatchingLocale
+ };
+ });
+ });
+}