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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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";
     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", {});
    13 const baseURI = rootURI + "locale/";
    14 const preferedLocales = getPreferedLocales(true);
    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 }
    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 }
    37 function get(key, n, locales) {
    38   // try this locale
    39   let locale = locales.shift();
    40   let localized;
    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     }
    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     }
    60     if (!localized) {
    61       localized = getKey(locale, key + '[other]');
    62     }
    63   }
    65   if (!localized) {
    66     localized = getKey(locale, key);
    67   }
    69   if (localized) {
    70     return localized;
    71   }
    73   // try next locale
    74   if (locales.length)
    75     return get(key, n, locales);
    77   return undefined;
    78 }
    79 exports.get = function(k, n) get(k, n, Array.slice(preferedLocales));

mercurial