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 * This module provides the PluralForm object which contains a method to figure
7 * out which plural form of a word to use for a given number based on the
8 * current localization. There is also a makeGetter method that creates a get
9 * function for the desired plural rule. This is useful for extensions that
10 * specify their own plural rule instead of relying on the browser default.
11 * (I.e., the extension hasn't been localized to the browser's locale.)
13 * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
15 * NOTE: any change to these plural forms need to be reflected in
17 * https://hg.mozilla.org/l10n/compare-locales/file/default/compare_locales/plurals.py
22 * get(int aNum, string aWords)
27 * [string pluralForm get(int aNum, string aWords), int numForms numForms()]
28 * makeGetter(int aRuleNum)
29 * Note: Basically, makeGetter returns 2 functions that do "get" and "numForm"
32 const kIntlProperties = "chrome://global/locale/intl.properties";
34 // These are the available plural functions that give the appropriate index
35 // based on the plural rule number specified. The first element is the number
36 // of plural forms and the second is the function to figure out the index.
37 /* eslint-disable no-nested-ternary */
42 [2, n => (n != 1 ? 1 : 0)],
44 [2, n => (n > 1 ? 1 : 0)],
46 [3, n => (n % 10 == 1 && n % 100 != 11 ? 1 : n % 10 == 0 ? 0 : 2)],
51 n == 1 || n == 11 ? 0 : n == 2 || n == 12 ? 1 : n > 0 && n < 20 ? 2 : 3,
54 [3, n => (n == 1 ? 0 : n == 0 || (n % 100 > 0 && n % 100 < 20) ? 1 : 2)],
59 n % 10 == 1 && n % 100 != 11
61 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20)
69 n % 10 == 1 && n % 100 != 11
71 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
76 [3, n => (n == 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2)],
83 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
95 : n % 100 == 3 || n % 100 == 4
123 : n % 100 >= 3 && n % 100 <= 10
125 : n % 100 >= 11 && n % 100 <= 99
135 : n == 0 || (n % 100 > 0 && n % 100 <= 10)
137 : n % 100 > 10 && n % 100 < 20
142 [3, n => (n % 10 == 1 ? 0 : n % 10 == 2 ? 1 : 2)],
143 // 15: Icelandic, Macedonian
144 [2, n => (n % 10 == 1 && n % 100 != 11 ? 0 : 1)],
149 n % 10 == 1 && n % 100 != 11 && n % 100 != 71 && n % 100 != 91
151 : n % 10 == 2 && n % 100 != 12 && n % 100 != 72 && n % 100 != 92
153 : (n % 10 == 3 || n % 10 == 4 || n % 10 == 9) &&
164 : n % 1000000 == 0 && n != 0
169 [2, n => (n != 0 ? 1 : 0)],
173 n => (n == 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n == 3 ? 3 : n == 6 ? 4 : 5),
175 // 19: Slavic languages (bs, hr, sr). Same as rule 7, but resulting in different CLDR categories
179 n % 10 == 1 && n % 100 != 11
181 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
187 /* eslint-enable no-nested-ternary */
189 export var PluralForm = {
191 * Get the correct plural form of a word based on the number
194 * The number to decide which plural form to use
196 * A semi-colon (;) separated string of words to pick the plural form
197 * @return The appropriate plural form of the word
200 // This method will lazily load to avoid perf when it is first needed and
201 // creates getPluralForm function. The function it creates is based on the
202 // value of pluralRule specified in the intl stringbundle.
203 // See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
205 // Delete the getters to be overwritten
206 delete PluralForm.numForms;
207 delete PluralForm.get;
209 // Make the plural form get function and set it as the default get
210 [PluralForm.get, PluralForm.numForms] = PluralForm.makeGetter(
213 return PluralForm.get;
217 * Create a pair of plural form functions for the given plural rule number.
220 * The plural rule number to create functions
221 * @return A pair: [function that gets the right plural form,
222 * function that returns the number of plural forms]
224 makeGetter(aRuleNum) {
225 // Default to "all plural" if the value is out of bounds or invalid
226 if (aRuleNum < 0 || aRuleNum >= gFunctions.length || isNaN(aRuleNum)) {
227 log(["Invalid rule number: ", aRuleNum, " -- defaulting to 0"]);
231 // Get the desired pluralRule function
232 let [numForms, pluralFunc] = gFunctions[aRuleNum];
234 // Return functions that give 1) the number of forms and 2) gets the right
237 function(aNum, aWords) {
238 // Figure out which index to use for the semi-colon separated words
239 let index = pluralFunc(aNum ? Number(aNum) : 0);
240 let words = aWords ? aWords.split(/;/) : [""];
242 // Explicitly check bounds to avoid strict warnings
243 let ret = index < words.length ? words[index] : undefined;
245 // Check for array out of bounds or empty strings
246 if (ret == undefined || ret == "") {
247 // Report the caller to help figure out who is causing badness
248 let caller = Components.stack.caller
249 ? Components.stack.caller.name
252 // Display a message in the error console
260 " is invalid -- plural rule #",
266 // Default to the first entry (which might be empty, but not undefined)
277 * Get the number of forms for the current plural rule
279 * @return The number of forms
282 // We lazily load numForms, so trigger the init logic with get()
284 return PluralForm.numForms;
288 * Get the plural rule number from the intl stringbundle
290 * @return The plural rule number
295 .createBundle(kIntlProperties)
296 .GetStringFromName("pluralRule")
302 * Private helper function to log errors to the error console and command line
305 * Error message to log or an array of strings to concat
308 let msg = "PluralForm.jsm: " + (aMsg.join ? aMsg.join("") : aMsg);
309 Services.console.logStringMessage(msg);