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

person-select: one i18next instance per element

parent c06f6541
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 {PersonSelect} from './person-select.js'; import {PersonSelect} from './person-select.js';
...@@ -10,7 +10,8 @@ import DBPLitElement from "@dbp-toolkit/common/dbp-lit-element"; ...@@ -10,7 +10,8 @@ import DBPLitElement from "@dbp-toolkit/common/dbp-lit-element";
export class PersonSelectDemo extends ScopedElementsMixin(DBPLitElement) { export class PersonSelectDemo extends ScopedElementsMixin(DBPLitElement) {
constructor() { constructor() {
super(); super();
this.lang = 'de'; this._i18n = createInstance();
this.lang = this._i18n.language;
this.entryPointUrl = ''; this.entryPointUrl = '';
this.noAuth = false; this.noAuth = false;
} }
...@@ -32,14 +33,6 @@ export class PersonSelectDemo extends ScopedElementsMixin(DBPLitElement) { ...@@ -32,14 +33,6 @@ export class PersonSelectDemo extends ScopedElementsMixin(DBPLitElement) {
}; };
} }
connectedCallback() {
super.connectedCallback();
i18n.changeLanguage(this.lang);
this.updateComplete.then(()=>{
});
}
static get styles() { static get styles() {
// language=css // language=css
return [ return [
...@@ -52,6 +45,13 @@ export class PersonSelectDemo extends ScopedElementsMixin(DBPLitElement) { ...@@ -52,6 +45,13 @@ export class PersonSelectDemo extends ScopedElementsMixin(DBPLitElement) {
]; ];
} }
update(changedProperties) {
if (changedProperties.has("lang")) {
this._i18n.changeLanguage(this.lang);
}
super.update(changedProperties);
}
getAuthComponentHtml() { getAuthComponentHtml() {
return this.noAuth ? html`<dbp-login-button subscribe="auth" lang="${this.lang}" show-image></dbp-login-button>` : html` return this.noAuth ? html`<dbp-login-button subscribe="auth" lang="${this.lang}" show-image></dbp-login-button>` : html`
<div class="container"> <div class="container">
......
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
...@@ -6,7 +6,7 @@ import select2LangEn from './i18n/en/select2'; ...@@ -6,7 +6,7 @@ import select2LangEn from './i18n/en/select2';
import JSONLD from '@dbp-toolkit/common/jsonld'; import JSONLD from '@dbp-toolkit/common/jsonld';
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 {i18n} from './i18n.js'; import {createInstance} from './i18n.js';
import {Icon} from '@dbp-toolkit/common'; import {Icon} from '@dbp-toolkit/common';
import * as commonUtils from '@dbp-toolkit/common/utils'; import * as commonUtils from '@dbp-toolkit/common/utils';
import * as commonStyles from '@dbp-toolkit/common/styles'; import * as commonStyles from '@dbp-toolkit/common/styles';
...@@ -29,7 +29,8 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) { ...@@ -29,7 +29,8 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) {
super(); super();
Object.assign(PersonSelect.prototype, errorUtils.errorMixin); Object.assign(PersonSelect.prototype, errorUtils.errorMixin);
this.auth = {}; this.auth = {};
this.lang = 'de'; this._i18n = createInstance();
this.lang = this._i18n.language;
this.entryPointUrl = ''; this.entryPointUrl = '';
this.jsonld = null; this.jsonld = null;
this.$select = null; this.$select = null;
...@@ -129,6 +130,7 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) { ...@@ -129,6 +130,7 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) {
* @param ignorePreset * @param ignorePreset
*/ */
initSelect2(ignorePreset = false) { initSelect2(ignorePreset = false) {
const i18n = this._i18n;
const that = this; const that = this;
const $this = $(this); const $this = $(this);
...@@ -277,7 +279,7 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) { ...@@ -277,7 +279,7 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) {
changedProperties.forEach((oldValue, propName) => { changedProperties.forEach((oldValue, propName) => {
switch (propName) { switch (propName) {
case "lang": case "lang":
i18n.changeLanguage(this.lang); this._i18n.changeLanguage(this.lang);
if (this.select2IsInitialized()) { if (this.select2IsInitialized()) {
// no other way to set an other language at runtime did work // no other way to set an other language at runtime did work
...@@ -361,6 +363,7 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) { ...@@ -361,6 +363,7 @@ export class PersonSelect extends ScopedElementsMixin(AdapterLitElement) {
} }
render() { render() {
const i18n = this._i18n;
const select2CSS = commonUtils.getAssetURL(select2CSSPath); const select2CSS = commonUtils.getAssetURL(select2CSSPath);
return html` return html`
<link rel="stylesheet" href="${select2CSS}"> <link rel="stylesheet" href="${select2CSS}">
......
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