|
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/. */ |
|
4 |
|
5 this.EXPORTED_SYMBOLS = [ "PluralForm" ]; |
|
6 |
|
7 /** |
|
8 * This module provides the PluralForm object which contains a method to figure |
|
9 * out which plural form of a word to use for a given number based on the |
|
10 * current localization. There is also a makeGetter method that creates a get |
|
11 * function for the desired plural rule. This is useful for extensions that |
|
12 * specify their own plural rule instead of relying on the browser default. |
|
13 * (I.e., the extension hasn't been localized to the browser's locale.) |
|
14 * |
|
15 * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals |
|
16 * |
|
17 * List of methods: |
|
18 * |
|
19 * string pluralForm |
|
20 * get(int aNum, string aWords) |
|
21 * |
|
22 * int numForms |
|
23 * numForms() |
|
24 * |
|
25 * [string pluralForm get(int aNum, string aWords), int numForms numForms()] |
|
26 * makeGetter(int aRuleNum) |
|
27 * Note: Basically, makeGetter returns 2 functions that do "get" and "numForm" |
|
28 */ |
|
29 |
|
30 const Cc = Components.classes; |
|
31 const Ci = Components.interfaces; |
|
32 |
|
33 const kIntlProperties = "chrome://global/locale/intl.properties"; |
|
34 |
|
35 // These are the available plural functions that give the appropriate index |
|
36 // based on the plural rule number specified. The first element is the number |
|
37 // of plural forms and the second is the function to figure out the index. |
|
38 let gFunctions = [ |
|
39 // 0: Chinese |
|
40 [1, function(n) 0], |
|
41 // 1: English |
|
42 [2, function(n) n!=1?1:0], |
|
43 // 2: French |
|
44 [2, function(n) n>1?1:0], |
|
45 // 3: Latvian |
|
46 [3, function(n) n%10==1&&n%100!=11?1:n!=0?2:0], |
|
47 // 4: Scottish Gaelic |
|
48 [4, function(n) n==1||n==11?0:n==2||n==12?1:n>0&&n<20?2:3], |
|
49 // 5: Romanian |
|
50 [3, function(n) n==1?0:n==0||n%100>0&&n%100<20?1:2], |
|
51 // 6: Lithuanian |
|
52 [3, function(n) n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?2:1], |
|
53 // 7: Russian |
|
54 [3, function(n) n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2], |
|
55 // 8: Slovak |
|
56 [3, function(n) n==1?0:n>=2&&n<=4?1:2], |
|
57 // 9: Polish |
|
58 [3, function(n) n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2], |
|
59 // 10: Slovenian |
|
60 [4, function(n) n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3], |
|
61 // 11: Irish Gaeilge |
|
62 [5, function(n) n==1?0:n==2?1:n>=3&&n<=6?2:n>=7&&n<=10?3:4], |
|
63 // 12: Arabic |
|
64 [6, function(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], |
|
65 // 13: Maltese |
|
66 [4, function(n) n==1?0:n==0||n%100>0&&n%100<=10?1:n%100>10&&n%100<20?2:3], |
|
67 // 14: Macedonian |
|
68 [3, function(n) n%10==1?0:n%10==2?1:2], |
|
69 // 15: Icelandic |
|
70 [2, function(n) n%10==1&&n%100!=11?0:1], |
|
71 // 16: Breton |
|
72 [5, function(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], |
|
73 ]; |
|
74 |
|
75 this.PluralForm = { |
|
76 /** |
|
77 * Get the correct plural form of a word based on the number |
|
78 * |
|
79 * @param aNum |
|
80 * The number to decide which plural form to use |
|
81 * @param aWords |
|
82 * A semi-colon (;) separated string of words to pick the plural form |
|
83 * @return The appropriate plural form of the word |
|
84 */ |
|
85 get get() |
|
86 { |
|
87 // This method will lazily load to avoid perf when it is first needed and |
|
88 // creates getPluralForm function. The function it creates is based on the |
|
89 // value of pluralRule specified in the intl stringbundle. |
|
90 // See: http://developer.mozilla.org/en/docs/Localization_and_Plurals |
|
91 |
|
92 // Delete the getters to be overwritten |
|
93 delete PluralForm.numForms; |
|
94 delete PluralForm.get; |
|
95 |
|
96 // Get the plural rule number from the intl stringbundle |
|
97 let ruleNum = Number(Cc["@mozilla.org/intl/stringbundle;1"]. |
|
98 getService(Ci.nsIStringBundleService).createBundle(kIntlProperties). |
|
99 GetStringFromName("pluralRule")); |
|
100 |
|
101 // Make the plural form get function and set it as the default get |
|
102 [PluralForm.get, PluralForm.numForms] = PluralForm.makeGetter(ruleNum); |
|
103 return PluralForm.get; |
|
104 }, |
|
105 |
|
106 /** |
|
107 * Create a pair of plural form functions for the given plural rule number. |
|
108 * |
|
109 * @param aRuleNum |
|
110 * The plural rule number to create functions |
|
111 * @return A pair: [function that gets the right plural form, |
|
112 * function that returns the number of plural forms] |
|
113 */ |
|
114 makeGetter: function(aRuleNum) |
|
115 { |
|
116 // Default to "all plural" if the value is out of bounds or invalid |
|
117 if (aRuleNum < 0 || aRuleNum >= gFunctions.length || isNaN(aRuleNum)) { |
|
118 log(["Invalid rule number: ", aRuleNum, " -- defaulting to 0"]); |
|
119 aRuleNum = 0; |
|
120 } |
|
121 |
|
122 // Get the desired pluralRule function |
|
123 let [numForms, pluralFunc] = gFunctions[aRuleNum]; |
|
124 |
|
125 // Return functions that give 1) the number of forms and 2) gets the right |
|
126 // plural form |
|
127 return [function(aNum, aWords) { |
|
128 // Figure out which index to use for the semi-colon separated words |
|
129 let index = pluralFunc(aNum ? Number(aNum) : 0); |
|
130 let words = aWords ? aWords.split(/;/) : [""]; |
|
131 |
|
132 // Explicitly check bounds to avoid strict warnings |
|
133 let ret = index < words.length ? words[index] : undefined; |
|
134 |
|
135 // Check for array out of bounds or empty strings |
|
136 if ((ret == undefined) || (ret == "")) { |
|
137 // Report the caller to help figure out who is causing badness |
|
138 let caller = PluralForm.get.caller ? PluralForm.get.caller.name : "top"; |
|
139 |
|
140 // Display a message in the error console |
|
141 log(["Index #", index, " of '", aWords, "' for value ", aNum, |
|
142 " is invalid -- plural rule #", aRuleNum, "; called by ", caller]); |
|
143 |
|
144 // Default to the first entry (which might be empty, but not undefined) |
|
145 ret = words[0]; |
|
146 } |
|
147 |
|
148 return ret; |
|
149 }, function() numForms]; |
|
150 }, |
|
151 |
|
152 /** |
|
153 * Get the number of forms for the current plural rule |
|
154 * |
|
155 * @return The number of forms |
|
156 */ |
|
157 get numForms() |
|
158 { |
|
159 // We lazily load numForms, so trigger the init logic with get() |
|
160 PluralForm.get(); |
|
161 return PluralForm.numForms; |
|
162 }, |
|
163 }; |
|
164 |
|
165 /** |
|
166 * Private helper function to log errors to the error console and command line |
|
167 * |
|
168 * @param aMsg |
|
169 * Error message to log or an array of strings to concat |
|
170 */ |
|
171 function log(aMsg) |
|
172 { |
|
173 let msg = "PluralForm.jsm: " + (aMsg.join ? aMsg.join("") : aMsg); |
|
174 Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService). |
|
175 logStringMessage(msg); |
|
176 dump(msg + "\n"); |
|
177 } |