From 7dffef4cc1b2a8fd189f8dd9deda7ea14a6f1ca1 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Thu, 1 Jul 2021 17:09:29 +0200
Subject: [PATCH] notification: one i18next instance per element

---
 .../notification/src/dbp-notification-demo.js | 21 ++++++++++++-------
 packages/notification/src/i18n.js             |  6 ++++--
 packages/notification/src/notification.js     |  9 +++-----
 3 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/packages/notification/src/dbp-notification-demo.js b/packages/notification/src/dbp-notification-demo.js
index 64d507b3..8919e31f 100644
--- a/packages/notification/src/dbp-notification-demo.js
+++ b/packages/notification/src/dbp-notification-demo.js
@@ -1,4 +1,4 @@
-import {i18n} from './i18n';
+import {createInstance} from './i18n';
 import {send as notify} from '@dbp-toolkit/common/notification';
 import {css, html, LitElement} from 'lit-element';
 import {ScopedElementsMixin} from '@open-wc/scoped-elements';
@@ -9,7 +9,8 @@ import * as commonStyles from "@dbp-toolkit/common/styles";
 export class NotificationDemo extends ScopedElementsMixin(LitElement) {
     constructor() {
         super();
-        this.lang = 'de';
+        this._i18n = createInstance();
+        this.lang = this._i18n.language;
     }
 
     static get scopedElements() {
@@ -24,13 +25,15 @@ export class NotificationDemo extends ScopedElementsMixin(LitElement) {
         };
     }
 
-    connectedCallback() {
-        super.connectedCallback();
-        i18n.changeLanguage(this.lang);
+    update(changedProperties) {
+        changedProperties.forEach((oldValue, propName) => {
+           if (propName === "lang") {
+               this._i18n.changeLanguage(this.lang);
+           }
+       });
 
-        this.updateComplete.then(()=>{
-        });
-    }
+       super.update(changedProperties);
+   }
 
     static get styles() {
         // language=css
@@ -42,6 +45,8 @@ export class NotificationDemo extends ScopedElementsMixin(LitElement) {
     }
 
     render() {
+        const i18n = this._i18n;
+
         return html`
             <section class="section">
                 <div class="container">
diff --git a/packages/notification/src/i18n.js b/packages/notification/src/i18n.js
index 498d9f03..975c1993 100644
--- a/packages/notification/src/i18n.js
+++ b/packages/notification/src/i18n.js
@@ -1,6 +1,8 @@
-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 en from './i18n/en/translation.json';
 
-export const i18n = createInstance({en: en, de: de}, 'de', 'en');
\ No newline at end of file
+export function createInstance() {
+    return _createInstance({en: en, de: de}, 'de', 'en');
+}
\ No newline at end of file
diff --git a/packages/notification/src/notification.js b/packages/notification/src/notification.js
index 9faf5efd..76a9c7a7 100644
--- a/packages/notification/src/notification.js
+++ b/packages/notification/src/notification.js
@@ -1,4 +1,4 @@
-import {i18n} from './i18n';
+import {createInstance} from './i18n';
 import {createUUID} from './utils';
 import {css, html} from 'lit-element';
 import DBPLitElement from '@dbp-toolkit/common/dbp-lit-element';
@@ -10,7 +10,8 @@ import * as commonStyles from '@dbp-toolkit/common/styles';
 export class Notification extends DBPLitElement {
     constructor() {
         super();
-        this.lang = 'de';
+        this._i18n = createInstance();
+        this.lang = this._i18n.language;
     }
 
     /**
@@ -25,7 +26,6 @@ export class Notification extends DBPLitElement {
 
     connectedCallback() {
         super.connectedCallback();
-        i18n.changeLanguage(this.lang);
         const that = this;
 
         window.addEventListener("dbp-notification-send", (e) => {
@@ -63,9 +63,6 @@ export class Notification extends DBPLitElement {
             // mark the event as handled
             e.preventDefault();
         });
-
-        this.updateComplete.then(()=>{
-        });
     }
 
     removeMessageId(messageElementId) {
-- 
GitLab