Skip to content

Node.js based forum software built for the modern web

resolve #8444 - Allow nested objects in translations (#8793)

Opliko6e430865583f
diff --git a/public/src/modules/translator.js b/public/src/modules/translator.js
index be9e89fa41..2fd50d5703 100644
--- a/public/src/modules/translator.js
+++ b/public/src/modules/translator.js
@@ -311,7 +311,28 @@
if (key) {
return translation.then(function (x) {
- return x[key];
+ 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 translation;
diff --git a/test/translator.js b/test/translator.js
index 0ec24949d9..1776c06b9b 100644
--- a/test/translator.js
+++ b/test/translator.js
@@ -330,4 +330,36 @@ describe('Translator static methods', function () {
assert.strictEqual(t, 'this is best a custom translation');
});
});
+
+ describe('translate nested keys', function () {
+ it('should handle nested translations', async function () {
+ shim.addTranslation('en-GB', 'my-namespace', {
+ key: {
+ key1: 'key1 translated',
+ key2: {
+ key3: 'key3 translated',
+ },
+ },
+ });
+ const t1 = await shim.translate('this is best [[my-namespace:key.key1]]');
+ const t2 = await shim.translate('this is best [[my-namespace:key.key2.key3]]');
+ assert.strictEqual(t1, 'this is best key1 translated');
+ assert.strictEqual(t2, 'this is best key3 translated');
+ });
+ it("should try the defaults if it didn't reach a string in a nested translation", async function () {
+ shim.addTranslation('en-GB', 'my-namespace', {
+ default1: {
+ default1: 'default1 translated',
+ '': 'incorrect priority',
+ },
+ default2: {
+ '': 'default2 translated',
+ },
+ });
+ const d1 = await shim.translate('this is best [[my-namespace:default1]]');
+ const d2 = await shim.translate('this is best [[my-namespace:default2]]');
+ assert.strictEqual(d1, 'this is best default1 translated');
+ assert.strictEqual(d2, 'this is best default2 translated');
+ });
+ });
});