Bug 1842974 - Remove dom.dialog_element.enabled pref r=emilio
[gecko.git] / devtools / shared / plural-form.js
blob1d89ad996154382aa2ed57b6a99222b683fc1cd7
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  * 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)
13  *
14  * Disable eslint warnings to preserve original code style.
15  */
17 /* eslint-disable */
19 /**
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.)
26  *
27  * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
28  *
29  * List of methods:
30  *
31  * string pluralForm
32  * get(int aNum, string aWords)
33  *
34  * int numForms
35  * numForms()
36  *
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"
40  */
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.
48 const gFunctions = [
49   // 0: Chinese
50   [1, (n) => 0],
51   // 1: English
52   [2, (n) => n!=1?1:0],
53   // 2: French
54   [2, (n) => n>1?1:0],
55   // 3: Latvian
56   [3, (n) => n%10==1&&n%100!=11?1:n%10==0?0:2],
57   // 4: Scottish Gaelic
58   [4, (n) => n==1||n==11?0:n==2||n==12?1:n>0&&n<20?2:3],
59   // 5: Romanian
60   [3, (n) => n==1?0:n==0||n%100>0&&n%100<20?1:2],
61   // 6: Lithuanian
62   [3, (n) => n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?2:1],
63   // 7: Russian
64   [3, (n) => n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
65   // 8: Slovak
66   [3, (n) => n==1?0:n>=2&&n<=4?1:2],
67   // 9: Polish
68   [3, (n) => n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
69   // 10: Slovenian
70   [4, (n) => n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3],
71   // 11: Irish Gaeilge
72   [5, (n) => n==1?0:n==2?1:n>=3&&n<=6?2:n>=7&&n<=10?3:4],
73   // 12: Arabic
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],
75   // 13: Maltese
76   [4, (n) => n==1?0:n==0||n%100>0&&n%100<=10?1:n%100>10&&n%100<20?2:3],
77   // 14: Unused
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],
81   // 16: Breton
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],
83   // 17: Shuar
84   [2, (n) => n!=0?1:0],
85   // 18: Welsh
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],
91 const PluralForm = {
92   /**
93    * Get the correct plural form of a word based on the number
94    *
95    * @param aNum
96    *        The number to decide which plural form to use
97    * @param aWords
98    *        A semi-colon (;) separated string of words to pick the plural form
99    * @return The appropriate plural form of the word
100    */
101   get get()
102   {
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;
110     delete this.get;
112     // Make the plural form get function and set it as the default get
113     [this.get, this.numForms] = this.makeGetter(this.ruleNum);
114     return this.get;
115   },
117   /**
118    * Create a pair of plural form functions for the given plural rule number.
119    *
120    * @param aRuleNum
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]
124    */
125   makeGetter: function(aRuleNum)
126   {
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"]);
130       aRuleNum = 0;
131     }
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
137     // plural form
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)
153         ret = words[0];
154       }
156       return ret;
157     }, () => numForms];
158   },
160   /**
161    * Get the number of forms for the current plural rule
162    *
163    * @return The number of forms
164    */
165   get numForms()
166   {
167     // We lazily load numForms, so trigger the init logic with get()
168     this.get();
169     return this.numForms;
170   },
172   /**
173    * Get the plural rule number from the intl stringbundle
174    *
175    * @return The plural rule number
176    */
177   get ruleNum()
178   {
179     try {
180       return parseInt(L10N.getStr("pluralRule"), 10);
181     } catch (e) {
182     // Fallback to English if the pluralRule property is not available.
183       return 1;
184     }
185   }
189  * Private helper function to log errors to the error console and command line
191  * @param aMsg
192  *        Error message to log or an array of strings to concat
193  */
194 function log(aMsg)
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;
203 /* eslint-enable */