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