Skip to content

Commit 3e2d50f

Browse files
authored
fix: allow setting the placeholder type for nls (#5584)
1 parent e5bea6f commit 3e2d50f

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

src/config_test.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ module.exports = {
5353
var nls = config.nls;
5454
config.setMessages({
5555
foo: "hello world of $1",
56-
test_key: "hello world for test key",
57-
test_with_curly_brackets: "hello world $0 of {1} and $2 to the {3} degree"
56+
test_key: "hello world for test key"
5857
});
5958
assert.equal(nls("untranslated_key","bar $1"), "bar $1");
6059
assert.equal(nls("untranslated_key", "bar"), "bar");
@@ -63,7 +62,25 @@ module.exports = {
6362
assert.equal(nls("untranslated_key", "$0B is $1$$", [0.11, 22]), "0.11B is 22$");
6463
assert.equal(nls("untranslated_key_but_translated_default_string", "foo", {1: "goo"}), "hello world of goo");
6564
assert.equal(nls("test_key", "this text should not appear"), "hello world for test key");
66-
assert.equal(nls("test_with_curly_brackets", "hello world $0 of {1} and $2 to the {3} degree", ["foo", "bar", "yay", "third"]), "hello world foo of bar and yay to the third degree");
65+
},
66+
"test: nls setting nlsPlaceholders": function() {
67+
var nls = config.nls;
68+
69+
// Should default to using dollar signs
70+
config.setMessages({
71+
test_with_curly_brackets: "hello world $0 of {0} and $1 to the {1} degree"
72+
});
73+
assert.equal(nls("test_with_curly_brackets", "hello world $0 of {1} and $1 to the {1} degree", ["bar", "third"]), "hello world bar of {0} and third to the {1} degree");
74+
75+
config.setMessages({
76+
test_with_curly_brackets: "hello world $0 of {0} and $1 to the {1} degree"
77+
}, {placeholders: "curlyBrackets"});
78+
assert.equal(nls("test_with_curly_brackets", "hello world $0 of {1} and $1 to the {1} degree", ["bar", "third"]), "hello world $0 of bar and $1 to the third degree");
79+
80+
config.setMessages({
81+
test_with_curly_brackets: "hello world $0 of {0} and $1 to the {1} degree"
82+
}, {placeholders: "dollarSigns"});
83+
assert.equal(nls("test_with_curly_brackets", "hello world $0 of {1} and $1 to the {1} degree", ["bar", "third"]), "hello world bar of {0} and third to the {1} degree");
6784
},
6885
"test: define options" : function() {
6986
var o = {};

src/lib/app_config.js

+20-10
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ function warn(message) {
5959
}
6060

6161
var messages;
62+
var nlsPlaceholders;
6263

6364
class AppConfig {
6465
constructor() {
6566
this.$defaultOptions = {};
6667
messages = defaultEnglishMessages;
68+
nlsPlaceholders = "dollarSigns";
6769
}
6870

6971
/**
@@ -138,9 +140,13 @@ class AppConfig {
138140

139141
/**
140142
* @param {any} value
143+
* @param {{placeholders?: "dollarSigns" | "curlyBrackets"}} [options]
141144
*/
142-
setMessages(value) {
145+
setMessages(value, options) {
143146
messages = value;
147+
if (options && options.placeholders) {
148+
nlsPlaceholders = options.placeholders;
149+
}
144150
}
145151

146152
/**
@@ -159,15 +165,19 @@ class AppConfig {
159165
var translated = messages[key] || messages[defaultString] || defaultString;
160166
if (params) {
161167
// We support both $n or {n} as placeholder indicators in the provided translated strings
162-
// Replace $n with the nth element in params
163-
translated = translated.replace(/\$(\$|[\d]+)/g, function(_, dollarMatch) {
164-
if (dollarMatch == "$") return "$";
165-
return params[dollarMatch];
166-
});
167-
// Replace {n} with the nth element in params
168-
translated = translated.replace(/\{([^\}]+)\}/g, function(_, curlyBracketMatch) {
169-
return params[curlyBracketMatch];
170-
});
168+
if (nlsPlaceholders === "dollarSigns") {
169+
// Replace $n with the nth element in params
170+
translated = translated.replace(/\$(\$|[\d]+)/g, function(_, dollarMatch) {
171+
if (dollarMatch == "$") return "$";
172+
return params[dollarMatch];
173+
});
174+
}
175+
if (nlsPlaceholders === "curlyBrackets") {
176+
// Replace {n} with the nth element in params
177+
translated = translated.replace(/\{([^\}]+)\}/g, function(_, curlyBracketMatch) {
178+
return params[curlyBracketMatch];
179+
});
180+
}
171181
}
172182
return translated;
173183
}

0 commit comments

Comments
 (0)