зеркало из https://github.com/mozilla/gecko-dev.git
Bug 416446 - Make PluralForm more useful for extensions and delay importing strings for perf. r=sdwilsh, r=smontagu, a1.9=damons
This commit is contained in:
Родитель
2b0c57ee05
Коммит
96237150e1
|
@ -39,7 +39,10 @@ EXPORTED_SYMBOLS = [ "PluralForm" ];
|
|||
/**
|
||||
* 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
|
||||
* current localization.
|
||||
* 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.)
|
||||
*
|
||||
* See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
|
||||
*
|
||||
|
@ -50,6 +53,10 @@ EXPORTED_SYMBOLS = [ "PluralForm" ];
|
|||
*
|
||||
* int numForms
|
||||
* numForms()
|
||||
*
|
||||
* [string pluralForm get(int aNum, string aWords), int numForms numForms()]
|
||||
* makeGetter(int aRuleNum)
|
||||
* Note: Basically, makeGetter returns 2 functions that do "get" and "numForm"
|
||||
*/
|
||||
|
||||
const Cc = Components.classes;
|
||||
|
@ -93,8 +100,6 @@ let gFunctions = [
|
|||
[3, function(n) n%10==1?0:n%10==2?1:2],
|
||||
];
|
||||
|
||||
let gNumForms;
|
||||
|
||||
let PluralForm = {
|
||||
/**
|
||||
* Get the correct plural form of a word based on the number
|
||||
|
@ -105,30 +110,49 @@ let PluralForm = {
|
|||
* A semi-colon (;) separated string of words to pick the plural form
|
||||
* @return The appropriate plural form of the word
|
||||
*/
|
||||
get: (function initGetPluralForm()
|
||||
get get()
|
||||
{
|
||||
// initGetPluralForm gets called right away when this module is loaded and
|
||||
// This method will lazily load to avoid perf when it is first needed and
|
||||
// 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
|
||||
|
||||
// Delete the getters to be overwritten
|
||||
delete PluralForm.numForms;
|
||||
delete PluralForm.get;
|
||||
|
||||
// Get the plural rule number from the intl stringbundle
|
||||
let ruleNum = Number(Cc["@mozilla.org/intl/stringbundle;1"].
|
||||
getService(Ci.nsIStringBundleService).createBundle(kIntlProperties).
|
||||
GetStringFromName("pluralRule"));
|
||||
|
||||
// Make the plural form get function and set it as the default get
|
||||
[PluralForm.get, PluralForm.numForms] = PluralForm.makeGetter(ruleNum);
|
||||
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)
|
||||
{
|
||||
// Default to "all plural" if the value is out of bounds or invalid
|
||||
if (ruleNum < 0 || ruleNum >= gFunctions.length || isNaN(ruleNum)) {
|
||||
log(["Invalid rule number: ", ruleNum, " -- defaulting to 0"]);
|
||||
ruleNum = 0;
|
||||
if (aRuleNum < 0 || aRuleNum >= gFunctions.length || isNaN(aRuleNum)) {
|
||||
log(["Invalid rule number: ", aRuleNum, " -- defaulting to 0"]);
|
||||
aRuleNum = 0;
|
||||
}
|
||||
|
||||
// Get the desired pluralRule function
|
||||
let pluralFunc;
|
||||
[gNumForms, pluralFunc] = gFunctions[ruleNum];
|
||||
let [numForms, pluralFunc] = gFunctions[aRuleNum];
|
||||
|
||||
// Return a function that gets the right plural form
|
||||
return function(aNum, aWords) {
|
||||
// Return functions that give 1) the number of forms and 2) gets the right
|
||||
// plural form
|
||||
return [function(aNum, aWords) {
|
||||
// Figure out which index to use for the semi-colon separated words
|
||||
let index = pluralFunc(aNum ? Number(aNum) : 0);
|
||||
let words = aWords ? aWords.split(/;/) : [""];
|
||||
|
@ -139,26 +163,31 @@ let PluralForm = {
|
|||
// Check for array out of bounds or empty strings
|
||||
if ((ret == undefined) || (ret == "")) {
|
||||
// Report the caller to help figure out who is causing badness
|
||||
let caller = this.get.caller ? this.get.caller.name : "top";
|
||||
let caller = PluralForm.get.caller ? PluralForm.get.caller.name : "top";
|
||||
|
||||
// Display a message in the error console
|
||||
log(["Index #", index, " of '", aWords, "' for value ", aNum,
|
||||
" is invalid -- plural rule #", ruleNum, "; called by ", caller]);
|
||||
" is invalid -- plural rule #", aRuleNum, "; called by ", caller]);
|
||||
|
||||
// Default to the first entry (which might be empty, but not undefined)
|
||||
ret = words[0];
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
})(),
|
||||
}, function() numForms];
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the number of forms for the current plural rule
|
||||
*
|
||||
* @return The number of forms
|
||||
*/
|
||||
numForms: function() gNumForms,
|
||||
get numForms()
|
||||
{
|
||||
// We lazily load numForms, so trigger the init logic with get()
|
||||
PluralForm.get();
|
||||
return PluralForm.numForms;
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Plural Form l10n Test Code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Edward Lee <edward.lee@engineering.uiuc.edu>.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2008
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* This unit test makes sure the plural form for Irish Gaeilge is working by
|
||||
* using the makeGetter method instead of using the default language (by
|
||||
* development), English.
|
||||
*/
|
||||
|
||||
Components.utils.import("resource://gre/modules/PluralForm.jsm");
|
||||
|
||||
function run_test()
|
||||
{
|
||||
// Irish is plural rule #11
|
||||
let [get, numForms] = PluralForm.makeGetter(11);
|
||||
|
||||
// Irish has 5 plural forms
|
||||
do_check_eq(5, numForms());
|
||||
|
||||
// I don't really know Irish, so I'll stick in some dummy text
|
||||
let words = "is 1;is 2;is 3-6;is 7-10;everything else";
|
||||
|
||||
let test = function(text, low, high) {
|
||||
for (let num = low; num <= high; num++)
|
||||
do_check_eq(text, get(num, words));
|
||||
};
|
||||
|
||||
// Make sure for good inputs, things work as expected
|
||||
test("everything else", 0, 0);
|
||||
test("is 1", 1, 1);
|
||||
test("is 2", 2, 2);
|
||||
test("is 3-6", 3, 6);
|
||||
test("is 7-10", 7, 10);
|
||||
test("everything else", 11, 200);
|
||||
}
|
Загрузка…
Ссылка в новой задаче