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

provider: one i18next instance per element

parent fce58155
No related branches found
No related tags found
1 merge request!67Create a new i18next instance for every web component
import {i18n} from './i18n.js'; import {createInstance} from './i18n.js';
import {css, html} from 'lit-element'; import {css, html} from 'lit-element';
import {ScopedElementsMixin} from '@open-wc/scoped-elements'; import {ScopedElementsMixin} from '@open-wc/scoped-elements';
import {AuthKeycloak, LoginButton} from '@dbp-toolkit/auth'; import {AuthKeycloak, LoginButton} from '@dbp-toolkit/auth';
...@@ -14,7 +14,8 @@ class ProviderDemo extends ScopedElementsMixin(DBPLitElement) { ...@@ -14,7 +14,8 @@ class ProviderDemo extends ScopedElementsMixin(DBPLitElement) {
constructor() { constructor() {
super(); super();
this.lang = 'de'; this._i18n = createInstance();
this.lang = this._i18n.language;
} }
static get scopedElements() { static get scopedElements() {
...@@ -35,18 +36,12 @@ class ProviderDemo extends ScopedElementsMixin(DBPLitElement) { ...@@ -35,18 +36,12 @@ class ProviderDemo extends ScopedElementsMixin(DBPLitElement) {
}; };
} }
connectedCallback() {
super.connectedCallback();
i18n.changeLanguage(this.lang);
}
attributeChangedCallback(name, oldValue, newValue) { attributeChangedCallback(name, oldValue, newValue) {
console.debug('ProviderDemo (' + this.id + ') attributeChangesCallback( ' + name + ', ' + oldValue + ', ' + newValue + ')'); console.debug('ProviderDemo (' + this.id + ') attributeChangesCallback( ' + name + ', ' + oldValue + ', ' + newValue + ')');
switch(name) { switch(name) {
case 'lang': case 'lang':
this.lang = newValue; this.lang = newValue;
i18n.changeLanguage(this.lang); this._i18n.changeLanguage(this.lang);
break; break;
default: default:
super.attributeChangedCallback(name, oldValue, newValue); super.attributeChangedCallback(name, oldValue, newValue);
...@@ -71,6 +66,7 @@ class ProviderDemo extends ScopedElementsMixin(DBPLitElement) { ...@@ -71,6 +66,7 @@ class ProviderDemo extends ScopedElementsMixin(DBPLitElement) {
} }
render() { render() {
const i18n = this._i18n;
return html` return html`
<section class="section"> <section class="section">
<p>${i18n.t('demo.provider_description', {id: "root", description: "is the top most in hierarchy"})}</p> <p>${i18n.t('demo.provider_description', {id: "root", description: "is the top most in hierarchy"})}</p>
...@@ -142,7 +138,8 @@ class DemoConsumer extends DBPLitElement { ...@@ -142,7 +138,8 @@ class DemoConsumer extends DBPLitElement {
constructor() { constructor() {
super(); super();
this.lang = 'de'; this._i18n = createInstance();
this.lang = this._i18n.language;
this.entryPointUrl = ''; this.entryPointUrl = '';
// default values // default values
this.foo = 100; this.foo = 100;
...@@ -157,7 +154,6 @@ class DemoConsumer extends DBPLitElement { ...@@ -157,7 +154,6 @@ class DemoConsumer extends DBPLitElement {
connectedCallback() { connectedCallback() {
super.connectedCallback(); super.connectedCallback();
i18n.changeLanguage(this.lang);
console.debug('DemoConsumer(' + this.id + ') connectedCallback()'); console.debug('DemoConsumer(' + this.id + ') connectedCallback()');
this.render(); this.render();
} }
...@@ -184,7 +180,7 @@ class DemoConsumer extends DBPLitElement { ...@@ -184,7 +180,7 @@ class DemoConsumer extends DBPLitElement {
switch(name) { switch(name) {
case 'lang': case 'lang':
this.lang = newValue; this.lang = newValue;
i18n.changeLanguage(this.lang); this._i18n.changeLanguage(this.lang);
break; break;
case 'foo': case 'foo':
this.foo = parseInt(newValue); this.foo = parseInt(newValue);
...@@ -209,6 +205,7 @@ class DemoConsumer extends DBPLitElement { ...@@ -209,6 +205,7 @@ class DemoConsumer extends DBPLitElement {
} }
render() { render() {
const i18n = this._i18n;
if (! this.connected) { if (! this.connected) {
return `not connected!`; return `not connected!`;
} }
......
import {createInstance} from '@dbp-toolkit/common/i18next.js'; import {createInstance as _createInstance} from '@dbp-toolkit/common/i18next.js';
import de from './i18n/de/translation.json'; import de from './i18n/de/translation.json';
import en from './i18n/en/translation.json'; import en from './i18n/en/translation.json';
export const i18n = createInstance({en: en, de: de}, 'de', 'en'); export function createInstance() {
\ No newline at end of file return _createInstance({en: en, de: de}, 'de', 'en');
}
\ No newline at end of file
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