Bug 1787947 - pref on CSS named pages in Nightly r=dholbert
[gecko.git] / intl / locale / PluralForm.sys.mjs
blobcd7f920aaaa13c2d85bb98838c9f2caccd4d2051
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 /**
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.)
12  *
13  * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
14  *
15  * NOTE: any change to these plural forms need to be reflected in
16  * compare-locales:
17  * https://hg.mozilla.org/l10n/compare-locales/file/default/compare_locales/plurals.py
18  *
19  * List of methods:
20  *
21  * string pluralForm
22  * get(int aNum, string aWords)
23  *
24  * int numForms
25  * numForms()
26  *
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"
30  */
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 */
38 var gFunctions = [
39   // 0: Chinese
40   [1, n => 0],
41   // 1: English
42   [2, n => (n != 1 ? 1 : 0)],
43   // 2: French
44   [2, n => (n > 1 ? 1 : 0)],
45   // 3: Latvian
46   [3, n => (n % 10 == 1 && n % 100 != 11 ? 1 : n % 10 == 0 ? 0 : 2)],
47   // 4: Scottish Gaelic
48   [
49     4,
50     n =>
51       n == 1 || n == 11 ? 0 : n == 2 || n == 12 ? 1 : n > 0 && n < 20 ? 2 : 3,
52   ],
53   // 5: Romanian
54   [3, n => (n == 1 ? 0 : n == 0 || (n % 100 > 0 && n % 100 < 20) ? 1 : 2)],
55   // 6: Lithuanian
56   [
57     3,
58     n =>
59       n % 10 == 1 && n % 100 != 11
60         ? 0
61         : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20)
62         ? 2
63         : 1,
64   ],
65   // 7: Russian
66   [
67     3,
68     n =>
69       n % 10 == 1 && n % 100 != 11
70         ? 0
71         : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
72         ? 1
73         : 2,
74   ],
75   // 8: Slovak
76   [3, n => (n == 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2)],
77   // 9: Polish
78   [
79     3,
80     n =>
81       n == 1
82         ? 0
83         : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
84         ? 1
85         : 2,
86   ],
87   // 10: Slovenian
88   [
89     4,
90     n =>
91       n % 100 == 1
92         ? 0
93         : n % 100 == 2
94         ? 1
95         : n % 100 == 3 || n % 100 == 4
96         ? 2
97         : 3,
98   ],
99   // 11: Irish Gaeilge
100   [
101     5,
102     n =>
103       n == 1
104         ? 0
105         : n == 2
106         ? 1
107         : n >= 3 && n <= 6
108         ? 2
109         : n >= 7 && n <= 10
110         ? 3
111         : 4,
112   ],
113   // 12: Arabic
114   [
115     6,
116     n =>
117       n == 0
118         ? 5
119         : n == 1
120         ? 0
121         : n == 2
122         ? 1
123         : n % 100 >= 3 && n % 100 <= 10
124         ? 2
125         : n % 100 >= 11 && n % 100 <= 99
126         ? 3
127         : 4,
128   ],
129   // 13: Maltese
130   [
131     4,
132     n =>
133       n == 1
134         ? 0
135         : n == 0 || (n % 100 > 0 && n % 100 <= 10)
136         ? 1
137         : n % 100 > 10 && n % 100 < 20
138         ? 2
139         : 3,
140   ],
141   // 14: Unused
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)],
145   // 16: Breton
146   [
147     5,
148     n =>
149       n % 10 == 1 && n % 100 != 11 && n % 100 != 71 && n % 100 != 91
150         ? 0
151         : n % 10 == 2 && n % 100 != 12 && n % 100 != 72 && n % 100 != 92
152         ? 1
153         : (n % 10 == 3 || n % 10 == 4 || n % 10 == 9) &&
154           n % 100 != 13 &&
155           n % 100 != 14 &&
156           n % 100 != 19 &&
157           n % 100 != 73 &&
158           n % 100 != 74 &&
159           n % 100 != 79 &&
160           n % 100 != 93 &&
161           n % 100 != 94 &&
162           n % 100 != 99
163         ? 2
164         : n % 1000000 == 0 && n != 0
165         ? 3
166         : 4,
167   ],
168   // 17: Shuar
169   [2, n => (n != 0 ? 1 : 0)],
170   // 18: Welsh
171   [
172     6,
173     n => (n == 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n == 3 ? 3 : n == 6 ? 4 : 5),
174   ],
175   // 19: Slavic languages (bs, hr, sr). Same as rule 7, but resulting in different CLDR categories
176   [
177     3,
178     n =>
179       n % 10 == 1 && n % 100 != 11
180         ? 0
181         : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)
182         ? 1
183         : 2,
184   ],
187 /* eslint-enable no-nested-ternary */
189 export var PluralForm = {
190   /**
191    * Get the correct plural form of a word based on the number
192    *
193    * @param aNum
194    *        The number to decide which plural form to use
195    * @param aWords
196    *        A semi-colon (;) separated string of words to pick the plural form
197    * @return The appropriate plural form of the word
198    */
199   get get() {
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(
211       PluralForm.ruleNum
212     );
213     return PluralForm.get;
214   },
216   /**
217    * Create a pair of plural form functions for the given plural rule number.
218    *
219    * @param aRuleNum
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]
223    */
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"]);
228       aRuleNum = 0;
229     }
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
235     // plural form
236     return [
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
250             : "top";
252           // Display a message in the error console
253           log([
254             "Index #",
255             index,
256             " of '",
257             aWords,
258             "' for value ",
259             aNum,
260             " is invalid -- plural rule #",
261             aRuleNum,
262             "; called by ",
263             caller,
264           ]);
266           // Default to the first entry (which might be empty, but not undefined)
267           ret = words[0];
268         }
270         return ret;
271       },
272       () => numForms,
273     ];
274   },
276   /**
277    * Get the number of forms for the current plural rule
278    *
279    * @return The number of forms
280    */
281   get numForms() {
282     // We lazily load numForms, so trigger the init logic with get()
283     PluralForm.get();
284     return PluralForm.numForms;
285   },
287   /**
288    * Get the plural rule number from the intl stringbundle
289    *
290    * @return The plural rule number
291    */
292   get ruleNum() {
293     return Number(
294       Services.strings
295         .createBundle(kIntlProperties)
296         .GetStringFromName("pluralRule")
297     );
298   },
302  * Private helper function to log errors to the error console and command line
304  * @param aMsg
305  *        Error message to log or an array of strings to concat
306  */
307 function log(aMsg) {
308   let msg = "PluralForm.jsm: " + (aMsg.join ? aMsg.join("") : aMsg);
309   Services.console.logStringMessage(msg);
310   dump(msg + "\n");