Skip to content

Node.js based forum software built for the modern web

fix: closes #14600, supported nested translations in tx()

Barış Soner Uşaklı251903efb172
diff --git a/public/src/modules/helpers.common.js b/public/src/modules/helpers.common.js
index 41686d31af..36351b8f35 100644
--- a/public/src/modules/helpers.common.js
+++ b/public/src/modules/helpers.common.js
@@ -74,7 +74,8 @@ module.exports = function (utils, Benchpress, tx, relative_path) {
args = argsFromToken;
}
const [namespace, key] = txToken.split(':', 2);
- if (!namespace || !key || !this?._i18n?.[namespace]?.[key]) {
+ const translation = tx.resolveKey(this?._i18n?.[namespace], key);
+ if (!namespace || !key || !translation) {
return tx.fixDoubleEscaped(tx.escapeHTML(token));
}
@@ -87,7 +88,6 @@ module.exports = function (utils, Benchpress, tx, relative_path) {
return escapedArg;
});
- const translation = this._i18n[namespace][key];
return tx.replaceArguments(translation, escapedArgs);
}
diff --git a/public/src/modules/translator.common.js b/public/src/modules/translator.common.js
index c450072506..6e17fefd19 100644
--- a/public/src/modules/translator.common.js
+++ b/public/src/modules/translator.common.js
@@ -26,6 +26,33 @@ module.exports = function (utils, load, warn) {
return parseTranslationString(str).some(token => token.tx);
}
+ // supports nested keys like 'nested-key.nested-1' and returns the value if found, otherwise returns false
+ function resolveKey(x, key) {
+ if (!x || !key) return false;
+ if (typeof x[key] === 'string') return x[key];
+ const keyParts = key.split('.');
+ for (let i = 0; i <= keyParts.length; i++) {
+ if (i === keyParts.length) {
+ // default to trying to find key with the same name as parent or equal to empty string
+ return x[keyParts[i - 1]] !== undefined ? x[keyParts[i - 1]] : x[''];
+ }
+ switch (typeof x[keyParts[i]]) {
+ case 'object':
+ x = x[keyParts[i]];
+ break;
+ case 'string':
+ if (i === keyParts.length - 1) {
+ return x[keyParts[i]];
+ }
+
+ return false;
+
+ default:
+ return false;
+ }
+ }
+ }
+
// takes token '[[topic:moved-from, arg1, arg2]]' and
// normalizes it to ['topic:moved-from', ['arg1', 'arg2']]
function normalizeToken(token, ignoreArgs = false) {
@@ -303,28 +330,7 @@ module.exports = function (utils, load, warn) {
if (key) {
return translation.then(function (x) {
- if (typeof x[key] === 'string') return x[key];
- const keyParts = key.split('.');
- for (let i = 0; i <= keyParts.length; i++) {
- if (i === keyParts.length) {
- // default to trying to find key with the same name as parent or equal to empty string
- return x[keyParts[i - 1]] !== undefined ? x[keyParts[i - 1]] : x[''];
- }
- switch (typeof x[keyParts[i]]) {
- case 'object':
- x = x[keyParts[i]];
- break;
- case 'string':
- if (i === keyParts.length - 1) {
- return x[keyParts[i]];
- }
-
- return false;
-
- default:
- return false;
- }
- }
+ return resolveKey(x, key);
});
}
return translation;
@@ -491,6 +497,7 @@ module.exports = function (utils, load, warn) {
},
isTranslationKey: isTranslationKey,
hasTranslationKey: hasTranslationKey,
+ resolveKey: resolveKey,
flush: function () {
Object.keys(Translator.cache).forEach(function (code) {
diff --git a/test/translator.js b/test/translator.js
index 27224f3380..2f69b89e4f 100644
--- a/test/translator.js
+++ b/test/translator.js
@@ -22,6 +22,9 @@ describe('Translator shim', () => {
context._i18n = await languages.getFull('en-GB');
context._i18n.topic['argument-test'] = 'Test arguments like %1 and %2, in them: %3';
context._i18n.topic['no-arguments'] = 'no arguments here';
+ context._i18n.topic['nested-key'] = {
+ 'nested-1': 'Nested key %1',
+ };
});
shim.addTranslation('en-GB', 'topic', {
@@ -91,9 +94,16 @@ describe('Translator shim', () => {
done();
});
- it('should translate nested keys with arguments', async () => {
+ it('should translate nested keys with arguments', (done) => {
const translated = helpers.tx.call(context, '[[notifications:new-message-in, [[modules:chat.room-id, 8]]]]');
assert.strictEqual(translated, 'New message in <strong>Room 8</strong>');
+ done();
+ });
+
+ it('should work with nested translation context', (done) => {
+ const translated = helpers.tx.call(context, '[[topic:nested-key.nested-1, foo]]');
+ assert.strictEqual(translated, 'Nested key foo');
+ done();
});
it('should html escape arguments but keep it if it\'s coming from tx file', (done) => {