Sat, 03 Jan 2015 20:18:00 +0100
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 module.metadata = {
7 "stability": "stable"
8 };
10 const json = require("./l10n/json/core");
11 const { get: getKey } = require("./l10n/core");
12 const properties = require("./l10n/properties/core");
13 const { getRulesForLocale } = require("./l10n/plural-rules");
15 // Retrieve the plural mapping function
16 let pluralMappingFunction = getRulesForLocale(json.language()) ||
17 getRulesForLocale("en");
19 exports.get = function get(k) {
20 // For now, we only accept a "string" as first argument
21 // TODO: handle plural forms in gettext pattern
22 if (typeof k !== "string")
23 throw new Error("First argument of localization method should be a string");
24 let n = arguments[1];
26 // Get translation from big hashmap or default to hard coded string:
27 let localized = getKey(k, n) || k;
29 // # Simplest usecase:
30 // // String hard coded in source code:
31 // _("Hello world")
32 // // Identifier of a key stored in properties file
33 // _("helloString")
34 if (arguments.length <= 1)
35 return localized;
37 let args = arguments;
39 if (typeof localized == "object" && "other" in localized) {
40 // # Plural form:
41 // // Strings hard coded in source code:
42 // _(["One download", "%d downloads"], 10);
43 // // Identifier of a key stored in properties file
44 // _("downloadNumber", 0);
45 let n = arguments[1];
47 // First handle simple universal forms that may not be mandatory
48 // for each language, (i.e. not different than 'other' form,
49 // but still usefull for better phrasing)
50 // For example 0 in english is the same form than 'other'
51 // but we accept 'zero' form if specified in localization file
52 if (n === 0 && "zero" in localized)
53 localized = localized["zero"];
54 else if (n === 1 && "one" in localized)
55 localized = localized["one"];
56 else if (n === 2 && "two" in localized)
57 localized = localized["two"];
58 else {
59 let pluralForm = pluralMappingFunction(n);
60 if (pluralForm in localized)
61 localized = localized[pluralForm];
62 else // Fallback in case of error: missing plural form
63 localized = localized["other"];
64 }
66 // Simulate a string with one placeholder:
67 args = [null, n];
68 }
70 // # String with placeholders:
71 // // Strings hard coded in source code:
72 // _("Hello %s", username)
73 // // Identifier of a key stored in properties file
74 // _("helloString", username)
75 // * We supports `%1s`, `%2s`, ... pattern in order to change arguments order
76 // in translation.
77 // * In case of plural form, we has `%d` instead of `%s`.
78 let offset = 1;
79 localized = localized.replace(/%(\d*)(s|d)/g, function (v, n) {
80 let rv = args[n != "" ? n : offset];
81 offset++;
82 return rv;
83 });
85 return localized;
86 }