mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-09-20 07:17:32 +09:00
Move Add-on SDK source to toolkit/jetpack
This commit is contained in:
parent
0a03067ee3
commit
23b67db438
993 changed files with 484 additions and 100663 deletions
70
toolkit/jetpack/sdk/l10n/loader.js
Normal file
70
toolkit/jetpack/sdk/l10n/loader.js
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/* 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/json/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
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue