mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-24 23:51:18 +01:00
* feat: #8444 Allow nested objects in translations * feat: #8444 Allow nested objects in translations * style: don't change formatting * fix: don't use the first string match if key isn't finished * feat: #8444 Allow nested objects in translations * feat: #8444 Allow nested objects in translations * style: don't change formatting * fix: don't use the first string match if key isn't finished * feat: add nested translations test
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user