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