1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Plural Form l10n Code.
16 * The Initial Developer of the Original Code is
17 * Edward Lee <edward.lee@engineering.uiuc.edu>.
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 var EXPORTED_SYMBOLS = [ "PluralForm" ];
40 * This module provides the PluralForm object which contains a method to figure
41 * out which plural form of a word to use for a given number based on the
42 * current localization. There is also a makeGetter method that creates a get
43 * function for the desired plural rule. This is useful for extensions that
44 * specify their own plural rule instead of relying on the browser default.
45 * (I.e., the extension hasn't been localized to the browser's locale.)
47 * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
52 * get(int aNum, string aWords)
57 * [string pluralForm get(int aNum, string aWords), int numForms numForms()]
58 * makeGetter(int aRuleNum)
59 * Note: Basically, makeGetter returns 2 functions that do "get" and "numForm"
62 const Cc = Components.classes;
63 const Ci = Components.interfaces;
65 const kIntlProperties = "chrome://global/locale/intl.properties";
67 // These are the available plural functions that give the appropriate index
68 // based on the plural rule number specified. The first element is the number
69 // of plural forms and the second is the function to figure out the index.
74 [2, function(n) n!=1?1:0],
76 [2, function(n) n>1?1:0],
78 [3, function(n) n%10==1&&n%100!=11?1:n!=0?2:0],
80 [3, function(n) n==1?0:n==2?1:2],
82 [3, function(n) n==1?0:n==0||n%100>0&&n%100<20?1:2],
84 [3, function(n) n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?2:1],
86 [3, function(n) n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
88 [3, function(n) n==1?0:n>=2&&n<=4?1:2],
90 [3, function(n) n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
92 [4, function(n) n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3],
94 [5, function(n) n==1?0:n==2?1:n>=3&&n<=6?2:n>=7&&n<=10?3:4],
96 [4, function(n) n==1?0:n==2?1:n<=10?2:3],
98 [4, function(n) n==1?0:n==0||n%100>0&&n%100<=10?1:n%100>10&&n%100<20?2:3],
100 [3, function(n) n%10==1?0:n%10==2?1:2],
105 * Get the correct plural form of a word based on the number
108 * The number to decide which plural form to use
110 * A semi-colon (;) separated string of words to pick the plural form
111 * @return The appropriate plural form of the word
115 // This method will lazily load to avoid perf when it is first needed and
116 // creates getPluralForm function. The function it creates is based on the
117 // value of pluralRule specified in the intl stringbundle.
118 // See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
120 // Delete the getters to be overwritten
121 delete PluralForm.numForms;
122 delete PluralForm.get;
124 // Get the plural rule number from the intl stringbundle
125 let ruleNum = Number(Cc["@mozilla.org/intl/stringbundle;1"].
126 getService(Ci.nsIStringBundleService).createBundle(kIntlProperties).
127 GetStringFromName("pluralRule"));
129 // Make the plural form get function and set it as the default get
130 [PluralForm.get, PluralForm.numForms] = PluralForm.makeGetter(ruleNum);
131 return PluralForm.get;
135 * Create a pair of plural form functions for the given plural rule number.
138 * The plural rule number to create functions
139 * @return A pair: [function that gets the right plural form,
140 * function that returns the number of plural forms]
142 makeGetter: function(aRuleNum)
144 // Default to "all plural" if the value is out of bounds or invalid
145 if (aRuleNum < 0 || aRuleNum >= gFunctions.length || isNaN(aRuleNum)) {
146 log(["Invalid rule number: ", aRuleNum, " -- defaulting to 0"]);
150 // Get the desired pluralRule function
151 let [numForms, pluralFunc] = gFunctions[aRuleNum];
153 // Return functions that give 1) the number of forms and 2) gets the right
155 return [function(aNum, aWords) {
156 // Figure out which index to use for the semi-colon separated words
157 let index = pluralFunc(aNum ? Number(aNum) : 0);
158 let words = aWords ? aWords.split(/;/) : [""];
160 // Explicitly check bounds to avoid strict warnings
161 let ret = index < words.length ? words[index] : undefined;
163 // Check for array out of bounds or empty strings
164 if ((ret == undefined) || (ret == "")) {
165 // Report the caller to help figure out who is causing badness
166 let caller = PluralForm.get.caller ? PluralForm.get.caller.name : "top";
168 // Display a message in the error console
169 log(["Index #", index, " of '", aWords, "' for value ", aNum,
170 " is invalid -- plural rule #", aRuleNum, "; called by ", caller]);
172 // Default to the first entry (which might be empty, but not undefined)
177 }, function() numForms];
181 * Get the number of forms for the current plural rule
183 * @return The number of forms
187 // We lazily load numForms, so trigger the init logic with get()
189 return PluralForm.numForms;
194 * Private helper function to log errors to the error console and command line
197 * Error message to log or an array of strings to concat
201 let msg = "PluralForm.jsm: " + (aMsg.join ? aMsg.join("") : aMsg);
202 Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).
203 logStringMessage(msg);