Skip to content

Commit

Permalink
fix: allow setting the placeholder type for nls (#5584)
Browse files Browse the repository at this point in the history
  • Loading branch information
akoreman committed Jun 11, 2024
1 parent e5bea6f commit 3e2d50f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
23 changes: 20 additions & 3 deletions src/config_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ module.exports = {
var nls = config.nls;
config.setMessages({
foo: "hello world of $1",
test_key: "hello world for test key",
test_with_curly_brackets: "hello world $0 of {1} and $2 to the {3} degree"
test_key: "hello world for test key"
});
assert.equal(nls("untranslated_key","bar $1"), "bar $1");
assert.equal(nls("untranslated_key", "bar"), "bar");
Expand All @@ -63,7 +62,25 @@ module.exports = {
assert.equal(nls("untranslated_key", "$0B is $1$$", [0.11, 22]), "0.11B is 22$");
assert.equal(nls("untranslated_key_but_translated_default_string", "foo", {1: "goo"}), "hello world of goo");
assert.equal(nls("test_key", "this text should not appear"), "hello world for test key");
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");
},
"test: nls setting nlsPlaceholders": function() {
var nls = config.nls;

// Should default to using dollar signs
config.setMessages({
test_with_curly_brackets: "hello world $0 of {0} and $1 to the {1} degree"
});
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");

config.setMessages({
test_with_curly_brackets: "hello world $0 of {0} and $1 to the {1} degree"
}, {placeholders: "curlyBrackets"});
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");

config.setMessages({
test_with_curly_brackets: "hello world $0 of {0} and $1 to the {1} degree"
}, {placeholders: "dollarSigns"});
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");
},
"test: define options" : function() {
var o = {};
Expand Down
30 changes: 20 additions & 10 deletions src/lib/app_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ function warn(message) {
}

var messages;
var nlsPlaceholders;

class AppConfig {
constructor() {
this.$defaultOptions = {};
messages = defaultEnglishMessages;
nlsPlaceholders = "dollarSigns";
}

/**
Expand Down Expand Up @@ -138,9 +140,13 @@ class AppConfig {

/**
* @param {any} value
* @param {{placeholders?: "dollarSigns" | "curlyBrackets"}} [options]
*/
setMessages(value) {
setMessages(value, options) {
messages = value;
if (options && options.placeholders) {
nlsPlaceholders = options.placeholders;
}
}

/**
Expand All @@ -159,15 +165,19 @@ class AppConfig {
var translated = messages[key] || messages[defaultString] || defaultString;
if (params) {
// We support both $n or {n} as placeholder indicators in the provided translated strings
// Replace $n with the nth element in params
translated = translated.replace(/\$(\$|[\d]+)/g, function(_, dollarMatch) {
if (dollarMatch == "$") return "$";
return params[dollarMatch];
});
// Replace {n} with the nth element in params
translated = translated.replace(/\{([^\}]+)\}/g, function(_, curlyBracketMatch) {
return params[curlyBracketMatch];
});
if (nlsPlaceholders === "dollarSigns") {
// Replace $n with the nth element in params
translated = translated.replace(/\$(\$|[\d]+)/g, function(_, dollarMatch) {
if (dollarMatch == "$") return "$";
return params[dollarMatch];
});
}
if (nlsPlaceholders === "curlyBrackets") {
// Replace {n} with the nth element in params
translated = translated.replace(/\{([^\}]+)\}/g, function(_, curlyBracketMatch) {
return params[curlyBracketMatch];
});
}
}
return translated;
}
Expand Down

0 comments on commit 3e2d50f

Please sign in to comment.