addon-sdk/source/lib/sdk/l10n/properties/core.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:30cf235ef3f8
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 const { Cu } = require("chrome");
7 const { newURI } = require('../../url/utils')
8 const { getRulesForLocale } = require("../plural-rules");
9 const { getPreferedLocales } = require('../locale');
10 const { rootURI } = require("@loader/options");
11 const { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
12
13 const baseURI = rootURI + "locale/";
14 const preferedLocales = getPreferedLocales(true);
15
16 function getLocaleURL(locale) {
17 // if the locale is a valid chrome URI, return it
18 try {
19 let uri = newURI(locale);
20 if (uri.scheme == 'chrome')
21 return uri.spec;
22 }
23 catch(_) {}
24 // otherwise try to construct the url
25 return baseURI + locale + ".properties";
26 }
27
28 function getKey(locale, key) {
29 let bundle = Services.strings.createBundle(getLocaleURL(locale));
30 try {
31 return bundle.GetStringFromName(key) + "";
32 }
33 catch (_) {}
34 return undefined;
35 }
36
37 function get(key, n, locales) {
38 // try this locale
39 let locale = locales.shift();
40 let localized;
41
42 if (typeof n == 'number') {
43 if (n == 0) {
44 localized = getKey(locale, key + '[zero]');
45 }
46 else if (n == 1) {
47 localized = getKey(locale, key + '[one]');
48 }
49 else if (n == 2) {
50 localized = getKey(locale, key + '[two]');
51 }
52
53 if (!localized) {
54 // Retrieve the plural mapping function
55 let pluralForm = (getRulesForLocale(locale.split("-")[0].toLowerCase()) ||
56 getRulesForLocale("en"))(n);
57 localized = getKey(locale, key + '[' + pluralForm + ']');
58 }
59
60 if (!localized) {
61 localized = getKey(locale, key + '[other]');
62 }
63 }
64
65 if (!localized) {
66 localized = getKey(locale, key);
67 }
68
69 if (localized) {
70 return localized;
71 }
72
73 // try next locale
74 if (locales.length)
75 return get(key, n, locales);
76
77 return undefined;
78 }
79 exports.get = function(k, n) get(k, n, Array.slice(preferedLocales));

mercurial