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

mercurial