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/. */
5 var EXPORTED_SYMBOLS = ["PluralForm"];
7 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
10 * This module provides the PluralForm object which contains a method to figure
11 * out which plural form of a word to use for a given number based on the
12 * current localization. There is also a makeGetter method that creates a get
13 * function for the desired plural rule. This is useful for extensions that
14 * specify their own plural rule instead of relying on the browser default.
15 * (I.e., the extension hasn't been localized to the browser's locale.)
17 * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
19 * NOTE: any change to these plural forms need to be reflected in
21 * https://hg.mozilla.org/l10n/compare-locales/file/default/compare_locales/plurals.py
26 * get(int aNum, string aWords)
31 * [string pluralForm get(int aNum, string aWords), int numForms numForms()]
32 * makeGetter(int aRuleNum)
33 * Note: Basically, makeGetter returns 2 functions that do "get" and "numForm"
36 const kIntlProperties = "chrome://global/locale/intl.properties";
38 // These are the available plural functions that give the appropriate index
39 // based on the plural rule number specified. The first element is the number
40 // of plural forms and the second is the function to figure out the index.
41 /* eslint-disable no-nested-ternary */
46 [2, n => (n != 1 ? 1 : 0)],
48 [2, n => (n > 1 ? 1 : 0)],
50 [3, n => (n % 10 == 1 && n % 100 != 11 ? 1 : n % 10 == 0 ? 0 : 2)],
55 n == 1 || n == 11 ? 0 : n == 2 || n == 12 ? 1 : n > 0 && n < 20 ? 2 : 3,
58 [3, n => (n == 1 ? 0 : n == 0 || (n % 100 > 0 && n % 100 < 20) ? 1 : 2)],
63 n % 10 == 1 && n % 100 != 11
65 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20)
73 n % 10 == 1 && n % 100 != 11
75 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
80 [3, n => (n == 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2)],
87 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
99 : n % 100 == 3 || n % 100 == 4
127 : n % 100 >= 3 && n % 100 <= 10
129 : n % 100 >= 11 && n % 100 <= 99
139 : n == 0 || (n % 100 > 0 && n % 100 <= 10)
141 : n % 100 > 10 && n % 100 < 20
146 [3, n => (n % 10 == 1 ? 0 : n % 10 == 2 ? 1 : 2)],
147 // 15: Icelandic, Macedonian
148 [2, n => (n % 10 == 1 && n % 100 != 11 ? 0 : 1)],
153 n % 10 == 1 && n % 100 != 11 && n % 100 != 71 && n % 100 != 91
155 : n % 10 == 2 && n % 100 != 12 && n % 100 != 72 && n % 100 != 92
157 : (n % 10 == 3 || n % 10 == 4 || n % 10 == 9) &&
168 : n % 1000000 == 0 && n != 0
173 [2, n => (n != 0 ? 1 : 0)],
177 n => (n == 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n == 3 ? 3 : n == 6 ? 4 : 5),
179 // 19: Slavic languages (bs, hr, sr). Same as rule 7, but resulting in different CLDR categories
183 n % 10 == 1 && n % 100 != 11
185 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
190 /* eslint-enable no-nested-ternary */
194 * Get the correct plural form of a word based on the number
197 * The number to decide which plural form to use
199 * A semi-colon (;) separated string of words to pick the plural form
200 * @return The appropriate plural form of the word
203 // This method will lazily load to avoid perf when it is first needed and
204 // creates getPluralForm function. The function it creates is based on the
205 // value of pluralRule specified in the intl stringbundle.
206 // See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
208 // Delete the getters to be overwritten
209 delete PluralForm.numForms;
210 delete PluralForm.get;
212 // Make the plural form get function and set it as the default get
213 [PluralForm.get, PluralForm.numForms] = PluralForm.makeGetter(
216 return PluralForm.get;
220 * Create a pair of plural form functions for the given plural rule number.
223 * The plural rule number to create functions
224 * @return A pair: [function that gets the right plural form,
225 * function that returns the number of plural forms]
227 makeGetter(aRuleNum) {
228 // Default to "all plural" if the value is out of bounds or invalid
229 if (aRuleNum < 0 || aRuleNum >= gFunctions.length || isNaN(aRuleNum)) {
230 log(["Invalid rule number: ", aRuleNum, " -- defaulting to 0"]);
234 // Get the desired pluralRule function
235 let [numForms, pluralFunc] = gFunctions[aRuleNum];
237 // Return functions that give 1) the number of forms and 2) gets the right
240 function(aNum, aWords) {
241 // Figure out which index to use for the semi-colon separated words
242 let index = pluralFunc(aNum ? Number(aNum) : 0);
243 let words = aWords ? aWords.split(/;/) : [""];
245 // Explicitly check bounds to avoid strict warnings
246 let ret = index < words.length ? words[index] : undefined;
248 // Check for array out of bounds or empty strings
249 if (ret == undefined || ret == "") {
250 // Report the caller to help figure out who is causing badness
251 let caller = Components.stack.caller
252 ? Components.stack.caller.name
255 // Display a message in the error console
263 " is invalid -- plural rule #",
269 // Default to the first entry (which might be empty, but not undefined)
280 * Get the number of forms for the current plural rule
282 * @return The number of forms
285 // We lazily load numForms, so trigger the init logic with get()
287 return PluralForm.numForms;
291 * Get the plural rule number from the intl stringbundle
293 * @return The plural rule number
298 .createBundle(kIntlProperties)
299 .GetStringFromName("pluralRule")
305 * Private helper function to log errors to the error console and command line
308 * Error message to log or an array of strings to concat
311 let msg = "PluralForm.jsm: " + (aMsg.join ? aMsg.join("") : aMsg);
312 Services.console.logStringMessage(msg);