michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: this.EXPORTED_SYMBOLS = [ "PluralForm" ]; michael@0: michael@0: /** michael@0: * This module provides the PluralForm object which contains a method to figure michael@0: * out which plural form of a word to use for a given number based on the michael@0: * current localization. There is also a makeGetter method that creates a get michael@0: * function for the desired plural rule. This is useful for extensions that michael@0: * specify their own plural rule instead of relying on the browser default. michael@0: * (I.e., the extension hasn't been localized to the browser's locale.) michael@0: * michael@0: * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals michael@0: * michael@0: * List of methods: michael@0: * michael@0: * string pluralForm michael@0: * get(int aNum, string aWords) michael@0: * michael@0: * int numForms michael@0: * numForms() michael@0: * michael@0: * [string pluralForm get(int aNum, string aWords), int numForms numForms()] michael@0: * makeGetter(int aRuleNum) michael@0: * Note: Basically, makeGetter returns 2 functions that do "get" and "numForm" michael@0: */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: const kIntlProperties = "chrome://global/locale/intl.properties"; michael@0: michael@0: // These are the available plural functions that give the appropriate index michael@0: // based on the plural rule number specified. The first element is the number michael@0: // of plural forms and the second is the function to figure out the index. michael@0: let gFunctions = [ michael@0: // 0: Chinese michael@0: [1, function(n) 0], michael@0: // 1: English michael@0: [2, function(n) n!=1?1:0], michael@0: // 2: French michael@0: [2, function(n) n>1?1:0], michael@0: // 3: Latvian michael@0: [3, function(n) n%10==1&&n%100!=11?1:n!=0?2:0], michael@0: // 4: Scottish Gaelic michael@0: [4, function(n) n==1||n==11?0:n==2||n==12?1:n>0&&n<20?2:3], michael@0: // 5: Romanian michael@0: [3, function(n) n==1?0:n==0||n%100>0&&n%100<20?1:2], michael@0: // 6: Lithuanian michael@0: [3, function(n) n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?2:1], michael@0: // 7: Russian michael@0: [3, function(n) n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2], michael@0: // 8: Slovak michael@0: [3, function(n) n==1?0:n>=2&&n<=4?1:2], michael@0: // 9: Polish michael@0: [3, function(n) n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2], michael@0: // 10: Slovenian michael@0: [4, function(n) n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3], michael@0: // 11: Irish Gaeilge michael@0: [5, function(n) n==1?0:n==2?1:n>=3&&n<=6?2:n>=7&&n<=10?3:4], michael@0: // 12: Arabic michael@0: [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], michael@0: // 13: Maltese michael@0: [4, function(n) n==1?0:n==0||n%100>0&&n%100<=10?1:n%100>10&&n%100<20?2:3], michael@0: // 14: Macedonian michael@0: [3, function(n) n%10==1?0:n%10==2?1:2], michael@0: // 15: Icelandic michael@0: [2, function(n) n%10==1&&n%100!=11?0:1], michael@0: // 16: Breton michael@0: [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], michael@0: ]; michael@0: michael@0: this.PluralForm = { michael@0: /** michael@0: * Get the correct plural form of a word based on the number michael@0: * michael@0: * @param aNum michael@0: * The number to decide which plural form to use michael@0: * @param aWords michael@0: * A semi-colon (;) separated string of words to pick the plural form michael@0: * @return The appropriate plural form of the word michael@0: */ michael@0: get get() michael@0: { michael@0: // This method will lazily load to avoid perf when it is first needed and michael@0: // creates getPluralForm function. The function it creates is based on the michael@0: // value of pluralRule specified in the intl stringbundle. michael@0: // See: http://developer.mozilla.org/en/docs/Localization_and_Plurals michael@0: michael@0: // Delete the getters to be overwritten michael@0: delete PluralForm.numForms; michael@0: delete PluralForm.get; michael@0: michael@0: // Get the plural rule number from the intl stringbundle michael@0: let ruleNum = Number(Cc["@mozilla.org/intl/stringbundle;1"]. michael@0: getService(Ci.nsIStringBundleService).createBundle(kIntlProperties). michael@0: GetStringFromName("pluralRule")); michael@0: michael@0: // Make the plural form get function and set it as the default get michael@0: [PluralForm.get, PluralForm.numForms] = PluralForm.makeGetter(ruleNum); michael@0: return PluralForm.get; michael@0: }, michael@0: michael@0: /** michael@0: * Create a pair of plural form functions for the given plural rule number. michael@0: * michael@0: * @param aRuleNum michael@0: * The plural rule number to create functions michael@0: * @return A pair: [function that gets the right plural form, michael@0: * function that returns the number of plural forms] michael@0: */ michael@0: makeGetter: function(aRuleNum) michael@0: { michael@0: // Default to "all plural" if the value is out of bounds or invalid michael@0: if (aRuleNum < 0 || aRuleNum >= gFunctions.length || isNaN(aRuleNum)) { michael@0: log(["Invalid rule number: ", aRuleNum, " -- defaulting to 0"]); michael@0: aRuleNum = 0; michael@0: } michael@0: michael@0: // Get the desired pluralRule function michael@0: let [numForms, pluralFunc] = gFunctions[aRuleNum]; michael@0: michael@0: // Return functions that give 1) the number of forms and 2) gets the right michael@0: // plural form michael@0: return [function(aNum, aWords) { michael@0: // Figure out which index to use for the semi-colon separated words michael@0: let index = pluralFunc(aNum ? Number(aNum) : 0); michael@0: let words = aWords ? aWords.split(/;/) : [""]; michael@0: michael@0: // Explicitly check bounds to avoid strict warnings michael@0: let ret = index < words.length ? words[index] : undefined; michael@0: michael@0: // Check for array out of bounds or empty strings michael@0: if ((ret == undefined) || (ret == "")) { michael@0: // Report the caller to help figure out who is causing badness michael@0: let caller = PluralForm.get.caller ? PluralForm.get.caller.name : "top"; michael@0: michael@0: // Display a message in the error console michael@0: log(["Index #", index, " of '", aWords, "' for value ", aNum, michael@0: " is invalid -- plural rule #", aRuleNum, "; called by ", caller]); michael@0: michael@0: // Default to the first entry (which might be empty, but not undefined) michael@0: ret = words[0]; michael@0: } michael@0: michael@0: return ret; michael@0: }, function() numForms]; michael@0: }, michael@0: michael@0: /** michael@0: * Get the number of forms for the current plural rule michael@0: * michael@0: * @return The number of forms michael@0: */ michael@0: get numForms() michael@0: { michael@0: // We lazily load numForms, so trigger the init logic with get() michael@0: PluralForm.get(); michael@0: return PluralForm.numForms; michael@0: }, michael@0: }; michael@0: michael@0: /** michael@0: * Private helper function to log errors to the error console and command line michael@0: * michael@0: * @param aMsg michael@0: * Error message to log or an array of strings to concat michael@0: */ michael@0: function log(aMsg) michael@0: { michael@0: let msg = "PluralForm.jsm: " + (aMsg.join ? aMsg.join("") : aMsg); michael@0: Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService). michael@0: logStringMessage(msg); michael@0: dump(msg + "\n"); michael@0: }