Bug 1301933 - Use english as the default plural form in PluralForm.jsm draft
authorJulian Descottes <jdescottes@mozilla.com>
Wed, 14 Sep 2016 12:27:59 +0200
changeset 413404 6da9d461a629
parent 413403 d8698c972acf
child 413405 3473293d7d68
push id29437
push userjdescottes@mozilla.com
push dateWed, 14 Sep 2016 10:37:51 +0000
bugs1301933
milestone51.0a1
Bug 1301933 - Use english as the default plural form in PluralForm.jsm MozReview-Commit-ID: 1gXVX0MDzri
intl/locale/PluralForm.jsm
intl/locale/tests/unit/test_pluralForm_makeGetter.js
--- a/intl/locale/PluralForm.jsm
+++ b/intl/locale/PluralForm.jsm
@@ -67,16 +67,18 @@ var gFunctions = [
   // 14: Macedonian
   [3, (n) => n%10==1?0:n%10==2?1:2],
   // 15: Icelandic
   [2, (n) => n%10==1&&n%100!=11?0:1],
   // 16: Breton
   [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],
 ];
 
+const [ENGLISH_FORMS, ENGLISH_FUNC] = gFunctions[1];
+
 this.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
@@ -115,19 +117,28 @@ this.PluralForm = {
     }
 
     // Get the desired pluralRule function
     let [numForms, pluralFunc] = gFunctions[aRuleNum];
 
     // Return functions that give 1) the number of forms and 2) gets the right
     // plural form
     return [function(aNum, aWords) {
+      aNum = aNum ? Number(aNum) : 0;
+      let words = aWords ? aWords.split(/;/) : [""];
+
+      let index;
       // Figure out which index to use for the semi-colon separated words
-      let index = pluralFunc(aNum ? Number(aNum) : 0);
-      let words = aWords ? aWords.split(/;/) : [""];
+      if (words.length != numForms && words.length == ENGLISH_FORMS) {
+        // Default to english if the number of plural forms in the string match english
+        // (i.e. 2) and don't match the current locale.
+        index = ENGLISH_FUNC(aNum);
+      } else {
+        index = pluralFunc(aNum);
+      }
 
       // 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 == "")) {
         // Report the caller to help figure out who is causing badness
         let caller = Components.stack.caller ? Components.stack.caller.name : "top";
--- a/intl/locale/tests/unit/test_pluralForm_makeGetter.js
+++ b/intl/locale/tests/unit/test_pluralForm_makeGetter.js
@@ -13,24 +13,32 @@ Components.utils.import("resource://gre/
 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));
+  let test = function (text, low, high, str) {
+    for (let num = low; num <= high; num++) {
+      do_check_eq(text, get(num, str));
+    }
   };
 
+  // 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";
   // 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);
+  test("everything else", 0, 0, words);
+  test("is 1", 1, 1, words);
+  test("is 2", 2, 2, words);
+  test("is 3-6", 3, 6, words);
+  test("is 7-10", 7, 10, words);
+  test("everything else", 11, 200, words);
+
+  let fallback = "one;more or zero";
+  test("more or zero", 0, 0, fallback);
+  test("one", 1, 1, fallback);
+  test("more or zero", 2, 2, fallback);
+  test("more or zero", 3, 6, fallback);
+  test("more or zero", 7, 10, fallback);
+  test("more or zero", 11, 200, fallback);
 }