Bug 413670 - Add plural rules for Irish ga-IE, Arabic, Maltese, Macedonian. r=l10n@mozilla.com (Pike), r=smontagu, a1.9=beltzner
authoredward.lee@engineering.uiuc.edu
Sat, 26 Jan 2008 23:33:05 -0800
changeset 10773 e108eb9b5724c7548938755ab1be99f72f796abe
parent 10772 ef765eea62ba04e20cbe205039f63bd8287396b4
child 10774 4a68b3e2458b140631ddf881f0f81077ff50ed38
push id1
push userbsmedberg@mozilla.com
push dateThu, 20 Mar 2008 16:49:24 +0000
treeherdermozilla-central@61007906a1f8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersl10n, smontagu
bugs413670
milestone1.9b3pre
Bug 413670 - Add plural rules for Irish ga-IE, Arabic, Maltese, Macedonian. r=l10n@mozilla.com (Pike), r=smontagu, a1.9=beltzner
intl/locale/src/PluralForm.jsm
intl/locale/tests/unit/test_pluralForm_english.js
--- a/intl/locale/src/PluralForm.jsm
+++ b/intl/locale/src/PluralForm.jsm
@@ -42,39 +42,64 @@ EXPORTED_SYMBOLS = [ "PluralForm" ];
  * current localization.
  *
  * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
  *
  * List of methods:
  *
  * string pluralForm
  * get(int aNum, string aWords)
+ *
+ * int numForms
+ * numForms()
  */
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
 const kIntlProperties = "chrome://global/locale/intl.properties";
 
 // These are the available plural functions that give the appropriate index
-// based on the plural rule number specified
+// 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.
 let gFunctions = [
-  function(n) 0,
-  function(n) n!=1?1:0,
-  function(n) n>1?1:0,
-  function(n) n%10==1&&n%100!=11?1:n!=0?2:0,
-  function(n) n==1?0:n==2?1:2,
-  function(n) n==1?0:n==0||n%100>0&&n%100<20?1:2,
-  function(n) n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?2:1,
-  function(n) n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2,
-  function(n) n==1?0:n>=2&&n<=4?1:2,
-  function(n) n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2,
-  function(n) n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3
+  // 0: Chinese
+  [1, function(n) 0],
+  // 1: English
+  [2, function(n) n!=1?1:0],
+  // 2: French
+  [2, function(n) n>1?1:0],
+  // 3: Latvian
+  [3, function(n) n%10==1&&n%100!=11?1:n!=0?2:0],
+  // 4: Scottish Gaelic
+  [3, function(n) n==1?0:n==2?1:2],
+  // 5: Romanian
+  [3, function(n) n==1?0:n==0||n%100>0&&n%100<20?1:2],
+  // 6: Lithuanian
+  [3, function(n) n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?2:1],
+  // 7: Russian
+  [3, function(n) n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
+  // 8: Slovak
+  [3, function(n) n==1?0:n>=2&&n<=4?1:2],
+  // 9: Polish
+  [3, function(n) n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
+  // 10: Slovenian
+  [4, function(n) n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3],
+  // 11: Irish Gaeilge
+  [5, function(n) n==1?0:n==2?1:n>=3&&n<=6?2:n>=7&&n<=10?3:4],
+  // 12: Arabic
+  [4, function(n) n==1?0:n==2?1:n<=10?2:3],
+  // 13: Maltese
+  [4, function(n) n==1?0:n==0||n%100>0&&n%100<=10?1:n%100>10&&n%100<20?2:3],
+  // 14: Macedonian
+  [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
    *
    * @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
@@ -93,38 +118,49 @@ let PluralForm = {
       GetStringFromName("pluralRule"));
 
     // 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;
     }
 
+    // Get the desired pluralRule function
+    let pluralFunc;
+    [gNumForms, pluralFunc] = gFunctions[ruleNum];
+
     // Return a function that gets the right plural form
-    let pluralFunc = gFunctions[ruleNum];
     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(/;/) : [""];
 
-      let ret = words[index];
+      // Explicitly check bounds to avoid strict warnings
+      let ret = index < words.length ? words[index] : undefined;
 
       // Check for array out of bounds or empty strings
       if ((ret == undefined) || (ret == "")) {
         // Display a message in the error console
         log(["Index #", index, " of '", aWords, "' for value ", aNum,
             " is invalid -- plural rule #", ruleNum]);
 
         // Default to the first entry (which might be empty, but not undefined)
         ret = words[0];
       }
 
       return ret;
     };
   })(),
+
+  /**
+   * Get the number of forms for the current plural rule
+   *
+   * @return The number of forms
+   */
+  numForms: function() gNumForms,
 };
 
 /**
  * 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
  */
--- a/intl/locale/tests/unit/test_pluralForm_english.js
+++ b/intl/locale/tests/unit/test_pluralForm_english.js
@@ -38,18 +38,21 @@
  * This unit test makes sure the plural form for the default language (by
  * development), English, is working for the PluralForm javascript module.
  */
 
 Components.utils.import("resource://gre/modules/PluralForm.jsm");
 
 function run_test()
 {
+  // English has 2 plural forms
+  do_check_eq(2, PluralForm.numForms());
+
   // Make sure for good inputs, things work as expected
-  for (var num = 0; num <= 1000; num++)
+  for (var num = 0; num <= 200; num++)
     do_check_eq(num == 1 ? "word" : "words", PluralForm.get(num, "word;words"));
 
   // Not having enough plural forms defaults to the first form
   do_check_eq("word", PluralForm.get(2, "word"));
 
   // Empty forms defaults to the first form
   do_check_eq("word", PluralForm.get(2, "word;"));
 }