--- 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();