mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 03:06:53 +08:00
FIX: Escape $ in translations before interpolating (#8100)
The dollar sign (`$`) is a special replace pattern, and `$&` inserts the matched string. Thus dollars signs need to be escaped with the special pattern `$$`, which inserts a single `$`.
This commit is contained in:
@ -116,7 +116,15 @@ I18n.interpolate = function(message, options) {
|
|||||||
for (var i = 0; placeholder = matches[i]; i++) {
|
for (var i = 0; placeholder = matches[i]; i++) {
|
||||||
name = placeholder.replace(this.PLACEHOLDER, "$1");
|
name = placeholder.replace(this.PLACEHOLDER, "$1");
|
||||||
|
|
||||||
value = options[name];
|
if (typeof options[name] === "string") {
|
||||||
|
// The dollar sign (`$`) is a special replace pattern, and `$&` inserts
|
||||||
|
// the matched string. Thus dollars signs need to be escaped with the
|
||||||
|
// special pattern `$$`, which inserts a single `$`.
|
||||||
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter
|
||||||
|
value = options[name].replace(/\$/g, "$$$$");
|
||||||
|
} else {
|
||||||
|
value = options[name];
|
||||||
|
}
|
||||||
|
|
||||||
if (!this.isValidNode(options, name)) {
|
if (!this.isValidNode(options, name)) {
|
||||||
value = "[missing " + placeholder + " value]";
|
value = "[missing " + placeholder + " value]";
|
||||||
|
@ -60,7 +60,8 @@ QUnit.module("lib:i18n", {
|
|||||||
days: {
|
days: {
|
||||||
one: "%{count} day",
|
one: "%{count} day",
|
||||||
other: "%{count} days"
|
other: "%{count} days"
|
||||||
}
|
},
|
||||||
|
dollar_sign: "Hi {{description}}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -223,3 +224,12 @@ QUnit.test("fallback", assert => {
|
|||||||
"falls back to English translations"
|
"falls back to English translations"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test("Dollar signs are properly escaped", assert => {
|
||||||
|
assert.equal(
|
||||||
|
I18n.t("dollar_sign", {
|
||||||
|
description: "$& $&"
|
||||||
|
}),
|
||||||
|
"Hi $& $&"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
Reference in New Issue
Block a user