Bug 974259 - Add getReplace and getReplaceAll helper functions to devtools's PluralForm. r?
MozReview-Commit-ID: JPuhZH4Py4P
--- a/devtools/shared/plural-form.js
+++ b/devtools/shared/plural-form.js
@@ -147,16 +147,52 @@ const 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 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()
this.get();