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

app-shell: one i18next instance per element

parent aa929f4e
No related branches found
No related tags found
1 merge request!67Create a new i18next instance for every web component
import {createI18nInstance} from './i18n.js'; import {createInstance} from './i18n.js';
import {html, css} from 'lit-element'; import {html, css} from 'lit-element';
import {ScopedElementsMixin} from '@open-wc/scoped-elements'; import {ScopedElementsMixin} from '@open-wc/scoped-elements';
import {LanguageSelect} from '@dbp-toolkit/language-select'; import {LanguageSelect} from '@dbp-toolkit/language-select';
...@@ -15,18 +15,16 @@ import {appWelcomeMeta} from './dbp-app-shell-welcome.js'; ...@@ -15,18 +15,16 @@ import {appWelcomeMeta} from './dbp-app-shell-welcome.js';
import {MatomoElement} from "@dbp-toolkit/matomo/src/matomo"; import {MatomoElement} from "@dbp-toolkit/matomo/src/matomo";
import DBPLitElement from "@dbp-toolkit/common/dbp-lit-element"; import DBPLitElement from "@dbp-toolkit/common/dbp-lit-element";
const i18n = createI18nInstance();
/** /**
* In case the application gets updated future dynamic imports might fail. * In case the application gets updated future dynamic imports might fail.
* This sends a notification suggesting the user to reload the page. * This sends a notification suggesting the user to reload the page.
* *
* uage: importNotify(import('<path>')); * usage: importNotify(import('<path>'));
* *
* @param i18n
* @param {Promise} promise * @param {Promise} promise
*/ */
const importNotify = async (promise) => { const importNotify = async (i18n, promise) => {
try { try {
return await promise; return await promise;
} catch (error) { } catch (error) {
...@@ -43,7 +41,6 @@ const importNotify = async (promise) => { ...@@ -43,7 +41,6 @@ const importNotify = async (promise) => {
export class AppShell extends ScopedElementsMixin(DBPLitElement) { export class AppShell extends ScopedElementsMixin(DBPLitElement) {
constructor() { constructor() {
super(); super();
this.lang = i18n.language;
this.activeView = ''; this.activeView = '';
this.entryPointUrl = ''; this.entryPointUrl = '';
this.subtitle = ''; this.subtitle = '';
...@@ -62,6 +59,8 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) { ...@@ -62,6 +59,8 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) {
this.buildTime = ''; this.buildTime = '';
this._loginStatus = 'unknown'; this._loginStatus = 'unknown';
this._roles = []; this._roles = [];
this._i18n = createInstance();
this.lang = this._i18n.language;
this.matomoUrl = ''; this.matomoUrl = '';
this.matomoSiteId = -1; this.matomoSiteId = -1;
...@@ -271,7 +270,7 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) { ...@@ -271,7 +270,7 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) {
*/ */
updateLangIfChanged(lang) { updateLangIfChanged(lang) {
// in case the language is unknown, fall back to the default // in case the language is unknown, fall back to the default
if (!i18n.languages.includes(lang)) { if (!this._i18n.languages.includes(lang)) {
lang = this.lang; lang = this.lang;
} }
if (this.lang !== lang) { if (this.lang !== lang) {
...@@ -287,10 +286,9 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) { ...@@ -287,10 +286,9 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) {
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);
// For screen readers // For screen readers
document.documentElement.setAttribute("lang", this.lang); document.documentElement.setAttribute("lang", this.lang);
i18n.changeLanguage(this.lang);
this.router.update(); this.router.update();
this.subtitle = this.activeMetaDataText("short_name"); this.subtitle = this.activeMetaDataText("short_name");
...@@ -420,7 +418,7 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) { ...@@ -420,7 +418,7 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) {
return; return;
} }
importNotify(import(metadata.module_src)).then(() => { importNotify(this._i18n, import(metadata.module_src)).then(() => {
updateFunc(); updateFunc();
}).catch((e) => { }).catch((e) => {
console.error(`Error loading ${ metadata.element }`); console.error(`Error loading ${ metadata.element }`);
...@@ -836,6 +834,8 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) { ...@@ -836,6 +834,8 @@ export class AppShell extends ScopedElementsMixin(DBPLitElement) {
} }
render() { render() {
let i18n = this._i18n;
const getSelectClasses = (name => { const getSelectClasses = (name => {
return classMap({selected: this.activeView === name}); return classMap({selected: this.activeView === name});
}); });
......
import {createI18nInstance} from './i18n.js'; import {createInstance} from './i18n.js';
import {html, css} from 'lit-element'; import {html, css} from 'lit-element';
import {unsafeHTML} from 'lit-html/directives/unsafe-html.js'; import {unsafeHTML} from 'lit-html/directives/unsafe-html.js';
import {ScopedElementsMixin} from '@open-wc/scoped-elements'; import {ScopedElementsMixin} from '@open-wc/scoped-elements';
...@@ -7,14 +7,12 @@ import {Icon} from '@dbp-toolkit/common'; ...@@ -7,14 +7,12 @@ import {Icon} from '@dbp-toolkit/common';
import {AdapterLitElement} from "@dbp-toolkit/provider/src/adapter-lit-element"; import {AdapterLitElement} from "@dbp-toolkit/provider/src/adapter-lit-element";
import {LoginStatus} from "@dbp-toolkit/auth/src/util"; import {LoginStatus} from "@dbp-toolkit/auth/src/util";
const i18n = createI18nInstance();
export class AuthMenuButton extends ScopedElementsMixin(AdapterLitElement) { export class AuthMenuButton extends ScopedElementsMixin(AdapterLitElement) {
constructor() { constructor() {
super(); super();
this.lang = 'de'; this._i18n = createInstance();
this.lang = this._i18n.language;
this.showImage = false; this.showImage = false;
this.auth = {}; this.auth = {};
...@@ -82,7 +80,7 @@ export class AuthMenuButton extends ScopedElementsMixin(AdapterLitElement) { ...@@ -82,7 +80,7 @@ export class AuthMenuButton extends ScopedElementsMixin(AdapterLitElement) {
update(changedProperties) { update(changedProperties) {
changedProperties.forEach((oldValue, propName) => { changedProperties.forEach((oldValue, propName) => {
if (propName === "lang") { if (propName === "lang") {
i18n.changeLanguage(this.lang); this._i18n.changeLanguage(this.lang);
} }
}); });
...@@ -246,6 +244,7 @@ export class AuthMenuButton extends ScopedElementsMixin(AdapterLitElement) { ...@@ -246,6 +244,7 @@ export class AuthMenuButton extends ScopedElementsMixin(AdapterLitElement) {
} }
renderLoggedIn() { renderLoggedIn() {
const i18n = this._i18n;
const person = this.auth.person; const person = this.auth.person;
const imageURL = (this.showImage && person && person.image) ? person.image : null; const imageURL = (this.showImage && person && person.image) ? person.image : null;
...@@ -270,6 +269,7 @@ export class AuthMenuButton extends ScopedElementsMixin(AdapterLitElement) { ...@@ -270,6 +269,7 @@ export class AuthMenuButton extends ScopedElementsMixin(AdapterLitElement) {
} }
renderLoggedOut() { renderLoggedOut() {
const i18n = this._i18n;
let loginSVG = ` let loginSVG = `
<svg <svg
viewBox="0 0 100 100" viewBox="0 0 100 100"
......
import {html , LitElement} from 'lit-element'; import {html , LitElement} from 'lit-element';
import {createI18nInstance} from './i18n.js'; import {createInstance} from './i18n.js';
import * as commonUtils from '@dbp-toolkit/common/utils'; import * as commonUtils from '@dbp-toolkit/common/utils';
const i18n = createI18nInstance();
class ActivityExample extends LitElement { class ActivityExample extends LitElement {
constructor() { constructor() {
super(); super();
this.lang = i18n.language; this._i18n = createInstance();
this.lang = this._i18n.language;
} }
static get properties() { static get properties() {
...@@ -21,7 +20,7 @@ class ActivityExample extends LitElement { ...@@ -21,7 +20,7 @@ class ActivityExample extends LitElement {
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);
break; break;
} }
}); });
...@@ -30,6 +29,7 @@ class ActivityExample extends LitElement { ...@@ -30,6 +29,7 @@ class ActivityExample extends LitElement {
} }
render() { render() {
const i18n = this._i18n;
return html` return html`
<h3>${i18n.t('activity-example.hello-world')}</h3> <h3>${i18n.t('activity-example.hello-world')}</h3>
<ul>${(Array.from(Array(100).keys())).map(i => html`<li>${i18n.t('activity-example.hello-world') + ' ' + i}</li>`)}</ul> <ul>${(Array.from(Array(100).keys())).map(i => html`<li>${i18n.t('activity-example.hello-world') + ' ' + i}</li>`)}</ul>
......
import {createI18nInstance} from './i18n.js'; import {createInstance} from './i18n.js';
import {css, html, LitElement} from 'lit-element'; import {css, html, LitElement} from 'lit-element';
import {ScopedElementsMixin} from '@open-wc/scoped-elements'; import {ScopedElementsMixin} from '@open-wc/scoped-elements';
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';
const i18n = createI18nInstance();
class AppShellWelcome extends ScopedElementsMixin(LitElement) { class AppShellWelcome extends ScopedElementsMixin(LitElement) {
constructor() { constructor() {
super(); super();
this.lang = i18n.language; this._i18n = createInstance();
this.lang = this._i18n.language;
this._onVisibilityChanged = this._onVisibilityChanged.bind(this); this._onVisibilityChanged = this._onVisibilityChanged.bind(this);
} }
...@@ -28,7 +27,7 @@ class AppShellWelcome extends ScopedElementsMixin(LitElement) { ...@@ -28,7 +27,7 @@ class AppShellWelcome extends ScopedElementsMixin(LitElement) {
update(changedProperties) { update(changedProperties) {
changedProperties.forEach((oldValue, propName) => { changedProperties.forEach((oldValue, propName) => {
if (propName === "lang") { if (propName === "lang") {
i18n.changeLanguage(this.lang); this._i18n.changeLanguage(this.lang);
} }
}); });
...@@ -75,6 +74,7 @@ class AppShellWelcome extends ScopedElementsMixin(LitElement) { ...@@ -75,6 +74,7 @@ class AppShellWelcome extends ScopedElementsMixin(LitElement) {
} }
render() { render() {
const i18n = this._i18n;
const app = AppShellWelcome._app; const app = AppShellWelcome._app;
let itemTemplates = []; let itemTemplates = [];
......
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';
const i18n = createInstance({en: en, de: de}, 'de', 'en'); export function createInstance() {
return _createInstance({en: en, de: de}, 'de', 'en');
export function createI18nInstance () {
return i18n.cloneInstance();
} }
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