Skip to content
Snippets Groups Projects
Commit b943a24c authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

translation overrides: Use the real tag name when looking for overrides

Instead of using the namespace which is hardcoded we use the tag name that was
used to register the element (or the "real" tag name in case of a scoped element).

This way if AppShell is regsitered as "dbp-some-app" it can still be overriden by
specifying "dbp-some-app" in the overrides object.
parent c46fef42
No related branches found
No related tags found
No related merge requests found
Pipeline #50931 passed
...@@ -100,20 +100,24 @@ export function createInstance(languages, lng, fallback, namespace) { ...@@ -100,20 +100,24 @@ export function createInstance(languages, lng, fallback, namespace) {
* object is equal to removing all overrides. * object is equal to removing all overrides.
* *
* @param {i18next.i18n} i18n - The i18next instance * @param {i18next.i18n} i18n - The i18next instance
* @param {object} overrides - The override data in the following format: "{language: {namespace: {key: value}}}" * @param {HTMLElement} element - The element at which the overrides are targeted
* @param {object} overrides - The override data in the following format: "{language: {tag-name: {key: value}}}"
*/ */
export function setOverrides(i18n, overrides) { export function setOverrides(i18n, element, overrides) {
// We add a special namespace which gets used with priority and falls back // We add a special namespace which gets used with priority and falls back
// to the original one. This way we an change the overrides at runtime // to the original one. This way we an change the overrides at runtime
// and can even remove them. // and can even remove them.
// The scoped mixin saves the real tag name under data-tag-name
let tagName = ((element.dataset && element.dataset.tagName) || element.tagName).toLowerCase();
let namespace = i18n.options.fallbackNS; let namespace = i18n.options.fallbackNS;
let overrideNamespace = getOverrideNamespace(namespace); let overrideNamespace = getOverrideNamespace(namespace);
let hasOverrides = false; let hasOverrides = false;
for(let lng of i18n.languages) { for(let lng of i18n.languages) {
i18n.removeResourceBundle(lng, overrideNamespace); i18n.removeResourceBundle(lng, overrideNamespace);
if (overrides[lng] === undefined || overrides[lng][namespace] === undefined) if (overrides[lng] === undefined || overrides[lng][tagName] === undefined)
continue; continue;
let resources = overrides[lng][namespace]; let resources = overrides[lng][tagName];
hasOverrides = true; hasOverrides = true;
i18n.addResourceBundle(lng, overrideNamespace, resources); i18n.addResourceBundle(lng, overrideNamespace, resources);
} }
......
...@@ -49,22 +49,23 @@ suite('i18next', () => { ...@@ -49,22 +49,23 @@ suite('i18next', () => {
}); });
test('overrides', () => { test('overrides', () => {
let namespace = 'ns'; let namespace = 'some-ns';
let element = document.createElement(namespace);
var inst = i18next.createInstance({en: {foo: 'bar'}}, 'en', 'en', namespace); var inst = i18next.createInstance({en: {foo: 'bar'}}, 'en', 'en', namespace);
assert.equal(inst.t('foo'), 'bar'); assert.equal(inst.t('foo'), 'bar');
assert.equal(inst.t('quux'), 'quux'); assert.equal(inst.t('quux'), 'quux');
i18next.setOverrides(inst, {en: {[namespace]: {quux: 'bla'}}}); i18next.setOverrides(inst, element, {en: {[namespace]: {quux: 'bla'}}});
assert.equal(inst.t('quux'), 'bla'); assert.equal(inst.t('quux'), 'bla');
assert.equal(inst.t('foo'), 'bar'); assert.equal(inst.t('foo'), 'bar');
i18next.setOverrides(inst, {en: {[namespace]: {}}}); i18next.setOverrides(inst, element, {en: {[namespace]: {}}});
assert.equal(inst.t('quux'), 'quux'); assert.equal(inst.t('quux'), 'quux');
assert.equal(inst.t('foo'), 'bar'); assert.equal(inst.t('foo'), 'bar');
i18next.setOverrides(inst, {en: {[namespace]: {foo: 'hmm'}}}); i18next.setOverrides(inst, element, {en: {[namespace]: {foo: 'hmm'}}});
assert.equal(inst.t('foo'), 'hmm'); assert.equal(inst.t('foo'), 'hmm');
i18next.setOverrides(inst, {en: {[namespace]: {quux: 'bla'}}}); i18next.setOverrides(inst, element, {en: {[namespace]: {quux: 'bla'}}});
assert.equal(inst.t('foo'), 'bar'); assert.equal(inst.t('foo'), 'bar');
assert.equal(inst.t('quux'), 'bla'); assert.equal(inst.t('quux'), 'bla');
i18next.setOverrides(inst, {}); i18next.setOverrides(inst, element, {});
assert.equal(inst.t('foo'), 'bar'); assert.equal(inst.t('foo'), 'bar');
assert.equal(inst.t('quux'), 'quux'); assert.equal(inst.t('quux'), 'quux');
}); });
......
...@@ -9,12 +9,12 @@ import {createInstance, setOverrides} from './i18n.js'; ...@@ -9,12 +9,12 @@ import {createInstance, setOverrides} from './i18n.js';
// This is an example on how to override translations at runtime // This is an example on how to override translations at runtime
let OVERRIDES = { let OVERRIDES = {
'de': { 'de': {
'translation': { 'dbp-language-select-display': {
'demo': 'Überschrieben' 'demo': 'Überschrieben'
} }
}, },
'en': { 'en': {
'translation': { 'dbp-language-select-display': {
'demo': 'Overridden' 'demo': 'Overridden'
} }
} }
...@@ -26,7 +26,7 @@ class LanguageSelectDisplay extends AdapterLitElement { ...@@ -26,7 +26,7 @@ class LanguageSelectDisplay extends AdapterLitElement {
super(); super();
this.lang = 'de'; this.lang = 'de';
this._i18n = createInstance(); this._i18n = createInstance();
setOverrides(this._i18n, OVERRIDES); setOverrides(this._i18n, this, OVERRIDES);
} }
static get properties() { static get properties() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment