Bug 1301933 - update devtools plural-form.js after change in original PluralForm.jsm draft
authorJulian Descottes <jdescottes@mozilla.com>
Wed, 14 Sep 2016 12:27:45 +0200
changeset 413405 3473293d7d68
parent 413404 6da9d461a629
child 531235 182cd6d6b28f
push id29437
push userjdescottes@mozilla.com
push dateWed, 14 Sep 2016 10:37:51 +0000
bugs1301933
milestone51.0a1
Bug 1301933 - update devtools plural-form.js after change in original PluralForm.jsm MozReview-Commit-ID: EcDT3yQ97mX
devtools/shared/plural-form.js
devtools/shared/tests/unit/test_pluralForm-makeGetter.js
--- a/devtools/shared/plural-form.js
+++ b/devtools/shared/plural-form.js
@@ -77,16 +77,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
@@ -125,19 +127,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 == "")) {
         // Display a message in the error console
         log(["Index #", index, " of '", aWords, "' for value ", aNum,
--- a/devtools/shared/tests/unit/test_pluralForm-makeGetter.js
+++ b/devtools/shared/tests/unit/test_pluralForm-makeGetter.js
@@ -14,25 +14,32 @@ const {PluralForm} = require("devtools/s
 
 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) {
+  let test = function (text, low, high, str) {
     for (let num = low; num <= high; num++) {
-      do_check_eq(text, get(num, words));
+      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);
 }