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/. */
6 * The code below is mostly is a slight modification of intl/locale/PluralForm.jsm that
7 * removes dependencies on chrome privileged APIs. To make maintenance easier, this file
8 * is kept as close as possible to the original in terms of implementation.
9 * The modified methods here are
10 * - makeGetter (remove code adding the caller name to the log)
11 * - get ruleNum() (rely on LocalizationHelper instead of String.services)
12 * - log() (rely on console.log)
14 * Disable eslint warnings to preserve original code style.
20 * This module provides the PluralForm object which contains a method to figure
21 * out which plural form of a word to use for a given number based on the
22 * current localization. There is also a makeGetter method that creates a get
23 * function for the desired plural rule. This is useful for extensions that
24 * specify their own plural rule instead of relying on the browser default.
25 * (I.e., the extension hasn't been localized to the browser's locale.)
27 * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
32 * get(int aNum, string aWords)
37 * [string pluralForm get(int aNum, string aWords), int numForms numForms()]
38 * makeGetter(int aRuleNum)
39 * Note: Basically, makeGetter returns 2 functions that do "get" and "numForm"
42 const { LocalizationHelper } = require("resource://devtools/shared/l10n.js");
43 const L10N = new LocalizationHelper("toolkit/locales/intl.properties");
45 // These are the available plural functions that give the appropriate index
46 // based on the plural rule number specified. The first element is the number
47 // of plural forms and the second is the function to figure out the index.
56 [3, (n) => n%10==1&&n%100!=11?1:n%10==0?0:2],
58 [4, (n) => n==1||n==11?0:n==2||n==12?1:n>0&&n<20?2:3],
60 [3, (n) => n==1?0:n==0||n%100>0&&n%100<20?1:2],
62 [3, (n) => n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?2:1],
64 [3, (n) => n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
66 [3, (n) => n==1?0:n>=2&&n<=4?1:2],
68 [3, (n) => n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
70 [4, (n) => n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3],
72 [5, (n) => n==1?0:n==2?1:n>=3&&n<=6?2:n>=7&&n<=10?3:4],
74 [6, (n) => n==0?5:n==1?0:n==2?1:n%100>=3&&n%100<=10?2:n%100>=11&&n%100<=99?3:4],
76 [4, (n) => n==1?0:n==0||n%100>0&&n%100<=10?1:n%100>10&&n%100<20?2:3],
78 [3, (n) => n%10==1?0:n%10==2?1:2],
79 // 15: Icelandic, Macedonian
80 [2, (n) => n%10==1&&n%100!=11?0:1],
82 [5, (n) => n%10==1&&n%100!=11&&n%100!=71&&n%100!=91?0:n%10==2&&n%100!=12&&n%100!=72&&n%100!=92?1:(n%10==3||n%10==4||n%10==9)&&n%100!=13&&n%100!=14&&n%100!=19&&n%100!=73&&n%100!=74&&n%100!=79&&n%100!=93&&n%100!=94&&n%100!=99?2:n%1000000==0&&n!=0?3:4],
86 [6, (n) => n==0?0:n==1?1:n==2?2:n==3?3:n==6?4:5],
87 // 19: Bosnian, Croatian, Serbian
88 [3, (n) => n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2],
93 * Get the correct plural form of a word based on the number
96 * The number to decide which plural form to use
98 * A semi-colon (;) separated string of words to pick the plural form
99 * @return The appropriate plural form of the word
103 // This method will lazily load to avoid perf when it is first needed and
104 // creates getPluralForm function. The function it creates is based on the
105 // value of pluralRule specified in the intl stringbundle.
106 // See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
108 // Delete the getters to be overwritten
109 delete this.numForms;
112 // Make the plural form get function and set it as the default get
113 [this.get, this.numForms] = this.makeGetter(this.ruleNum);
118 * Create a pair of plural form functions for the given plural rule number.
121 * The plural rule number to create functions
122 * @return A pair: [function that gets the right plural form,
123 * function that returns the number of plural forms]
125 makeGetter: function(aRuleNum)
127 // Default to "all plural" if the value is out of bounds or invalid
128 if (aRuleNum < 0 || aRuleNum >= gFunctions.length || isNaN(aRuleNum)) {
129 log(["Invalid rule number: ", aRuleNum, " -- defaulting to 0"]);
133 // Get the desired pluralRule function
134 let [numForms, pluralFunc] = gFunctions[aRuleNum];
136 // Return functions that give 1) the number of forms and 2) gets the right
138 return [function(aNum, aWords) {
139 // Figure out which index to use for the semi-colon separated words
140 let index = pluralFunc(aNum ? Number(aNum) : 0);
141 let words = aWords ? aWords.split(/;/) : [""];
143 // Explicitly check bounds to avoid strict warnings
144 let ret = index < words.length ? words[index] : undefined;
146 // Check for array out of bounds or empty strings
147 if ((ret == undefined) || (ret == "")) {
148 // Display a message in the error console
149 log(["Index #", index, " of '", aWords, "' for value ", aNum,
150 " is invalid -- plural rule #", aRuleNum, ";"]);
152 // Default to the first entry (which might be empty, but not undefined)
161 * Get the number of forms for the current plural rule
163 * @return The number of forms
167 // We lazily load numForms, so trigger the init logic with get()
169 return this.numForms;
173 * Get the plural rule number from the intl stringbundle
175 * @return The plural rule number
180 return parseInt(L10N.getStr("pluralRule"), 10);
182 // Fallback to English if the pluralRule property is not available.
189 * Private helper function to log errors to the error console and command line
192 * Error message to log or an array of strings to concat
196 let msg = "plural-form.js: " + (aMsg.join ? aMsg.join("") : aMsg);
197 console.log(msg + "\n");
200 exports.PluralForm = PluralForm;
201 exports.get = PluralForm.get;