Bug 974259 - Add helper functions to PluralForm.jsm for common tasks. r? draft
authorIan Moody <moz-ian@perix.co.uk>
Sun, 08 Oct 2017 22:01:46 +0100
changeset 677029 1ab831fefd9490167e1683b2e6c6cee03eb3a0e3
parent 676713 613f64109bdef590b9748355441b3c620efa7be5
child 677030 4cb03c2e7d4087fcbb4f7a296dbf8762827463f3
push id83667
push usermoz-ian@perix.co.uk
push dateMon, 09 Oct 2017 22:03:09 +0000
bugs974259
milestone58.0a1
Bug 974259 - Add helper functions to PluralForm.jsm for common tasks. r? PluralForm is commonly used with a string fetched from a stringbundle, and also frequently has simple replace operations performed on the result, e.g. PluralForm.get(num, words).replace("#1", num); Add helper functions for these operations to simplify common use. MozReview-Commit-ID: LYo8hz5003q
intl/locale/PluralForm.jsm
--- a/intl/locale/PluralForm.jsm
+++ b/intl/locale/PluralForm.jsm
@@ -20,16 +20,34 @@ this.EXPORTED_SYMBOLS = [ "PluralForm" ]
  * get(int aNum, string aWords)
  *
  * 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"
+ *
+ * string pluralForm
+ * getReplace(int aNum, string aStrings)
+ *
+ * string pluralForm
+ * getReplaceAll(int aNum, string aStrings, Array aParams)
+ *
+ * string pluralForm
+ * getFromBundle(int aNum, stringbundle aBundle, string aKey)
+ *
+ * string pluralForm
+ * getReplaceFromBundle(int aNum, stringbundle aBundle, string aKey)
+ *
+ * string pluralForm
+ * getReplaceAllFromBundle(int aNum, stringbundle aBundle, string aKey, Array aParams)
+ *
+ * int ruleNum
+ * ruleNum()
  */
 
 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
@@ -140,16 +158,108 @@ this.PluralForm = {
         ret = words[0];
       }
 
       return ret;
     }, () => numForms];
   },
 
   /**
+   * Get the correct plural form of a string based on the number, with "#1" in
+   * the string replaced by said number
+   *
+   * @param aNum {int}
+   *        The number to decide which plural form to use
+   * @param aStrings {String}
+   *        A semi-colon (;) separated string of strings to pick the plural form
+   * @returns The appropriate plural form of the string with "#1" replaced with aNum
+   */
+  getReplace(aNum, aStrings)
+  {
+    return PluralForm.get(aNum, aStrings).replace("#1", aNum);
+  },
+
+  /**
+   * Get the correct plural form of a string based on the number, and with
+   * placeholders replaced
+   *
+   * @param aNum {int}
+   *        The number to decide which plural form to use
+   * @param aStrings {String}
+   *        A semi-colon (;) separated string of words to pick the plural form
+   * @param aParams {Array}
+   *        The replacements for the #1 etc. placeholders
+   * @returns The appropriate plural form of the string with substitutions performed
+   */
+  getReplaceAll(aNum, aStrings, aParams)
+  {
+    let string = PluralForm.get(aNum, aStrings);
+    return string.replace(/\#(\d+)/g, function(matchedId, matchedNumber) {
+      let param = aParams[parseInt(matchedNumber, 10) - 1];
+      return param !== undefined ? param : matchedId;
+    });
+  },
+
+  /**
+   * Get the correct plural form of a string in a stringbundle based on the number
+   *
+   * @param aNum {int}
+   *        The number to decide which plural form to use
+   * @param aBundle {<xul:stringbundle>|nsIStringBundle}
+   *        The bundle to retrieve the strings from
+   * @param aKey {String}
+   *        The key of the string in the bundle
+   * @returns The appropriate plural form of the string
+   */
+  getFromBundle(aNum, aBundle, aKey)
+  {
+    return (aBundle.getString) ?
+      PluralForm.get(aNum, aBundle.getString(aKey)) :
+      PluralForm.get(aNum, aBundle.GetStringFromName(aKey));
+  },
+
+  /**
+   * Get the correct plural form of a string in a stringbundle based on the number,
+   * with "#1" in the string replaced with the number
+   *
+   * @param aNum {int}
+   *        The number to decide which plural form to use
+   * @param aBundle {<xul:stringbundle>|nsIStringBundle}
+   *        The bundle to retrieve the strings from
+   * @param aKey {String}
+   *        The key of the string in the bundle
+   * @returns The appropriate plural form of the string with "#1" replaced with aNum
+   */
+  getReplaceFromBundle(aNum, aBundle, aKey)
+  {
+    return PluralForm.getFromBundle(aNum, aBundle, aKey).replace("#1", aNum);
+  },
+
+  /**
+   * Get the correct plural form of a string in a stringbundle based on the number,
+   * and with placeholders replaced
+   *
+   * @param aNum {int}
+   *        The number to decide which plural form to use
+   * @param aBundle {<xul:stringbundle>|nsIStringBundle}
+   *        The bundle to retrieve the strings from
+   * @param aKey {String}
+   *        The key of the string in the bundle
+   * @param aParams {Array}
+   *        The replacements for the #1 etc. placeholders
+   * @returns The appropriate plural form of the string with substitutions performed
+   */
+  getReplaceAllFromBundle(aNum, aBundle, aKey, aParams)
+  {
+    let strings = (aBundle.getString) ? aBundle.getString(aKey) :
+                                        aBundle.GetStringFromName(aKey);
+    return PluralForm.getReplaceAll(aNum, strings, aParams);
+  },
+
+  /**
    * Get the number of forms for the current plural rule
    *
    * @return The number of forms
    */
   get numForms()
   {
     // We lazily load numForms, so trigger the init logic with get()
     PluralForm.get();