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

* 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
v1.18.x
Opliko 4 years ago committed by GitHub
parent fbdde03217
commit 6e43086558
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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');
});
});
});

Loading…
Cancel
Save