From 73b45d347fe5c52031d376c6433362d07a9a9ed9 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Thu, 10 Jun 2021 16:23:44 +0200
Subject: [PATCH] checkin-place-select: port to one i18next instance per
 component instance

---
 packages/check-in-place-select/i18next-scanner.config.js  | 1 +
 .../check-in-place-select/src/check-in-place-select.js    | 8 +++++---
 .../src/dbp-check-in-place-select-demo.js                 | 5 -----
 packages/check-in-place-select/src/i18n.js                | 8 ++++++--
 .../check-in-place-select/src/i18n/de/translation.json    | 1 -
 .../check-in-place-select/src/i18n/en/translation.json    | 1 -
 6 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/packages/check-in-place-select/i18next-scanner.config.js b/packages/check-in-place-select/i18next-scanner.config.js
index 8c277798..aeb8fdb6 100644
--- a/packages/check-in-place-select/i18next-scanner.config.js
+++ b/packages/check-in-place-select/i18next-scanner.config.js
@@ -6,6 +6,7 @@ module.exports = {
     options: {
         debug: false,
         removeUnusedKeys: true,
+        func: {list: ['i18n.t', '_i18n.t']},
         lngs: ['en','de'],
         resource: {
             loadPath: 'src/i18n/{{lng}}/{{ns}}.json',
diff --git a/packages/check-in-place-select/src/check-in-place-select.js b/packages/check-in-place-select/src/check-in-place-select.js
index 7b63bb9a..f2ed817d 100644
--- a/packages/check-in-place-select/src/check-in-place-select.js
+++ b/packages/check-in-place-select/src/check-in-place-select.js
@@ -4,7 +4,7 @@ import select2LangEn from './i18n/en/select2';
 import JSONLD from '@dbp-toolkit/common/jsonld';
 import {css, html} from 'lit-element';
 import {ScopedElementsMixin} from '@open-wc/scoped-elements';
-import {i18n} from './i18n.js';
+import {createInstance} from './i18n.js';
 import {Icon} from '@dbp-toolkit/common';
 import * as commonUtils from '@dbp-toolkit/common/utils';
 import * as commonStyles from '@dbp-toolkit/common/styles';
@@ -41,6 +41,7 @@ export class CheckInPlaceSelect extends ScopedElementsMixin(AdapterLitElement) {
         this.reloadButtonTitle = '';
         this.showCapacity = false;
         this.auth = {};
+        this._i18n = createInstance();
 
         this._onDocumentClicked = this._onDocumentClicked.bind(this);
     }
@@ -159,7 +160,7 @@ export class CheckInPlaceSelect extends ScopedElementsMixin(AdapterLitElement) {
             width: '100%',
             language: this.lang === "de" ? select2LangDe() : select2LangEn(),
             minimumInputLength: 2,
-            placeholder: i18n.t('check-in-place-select.placeholder'),
+            placeholder: this._i18n.t('check-in-place-select.placeholder'),
             dropdownParent: this.$('#check-in-place-select-dropdown'),
             ajax: {
                 delay: 500,
@@ -291,7 +292,7 @@ export class CheckInPlaceSelect extends ScopedElementsMixin(AdapterLitElement) {
         changedProperties.forEach((oldValue, propName) => {
             switch (propName) {
                 case "lang":
-                    i18n.changeLanguage(this.lang);
+                    this._i18n.changeLanguage(this.lang);
 
                     if (this.select2IsInitialized()) {
                         // no other way to set an other language at runtime did work
@@ -377,6 +378,7 @@ export class CheckInPlaceSelect extends ScopedElementsMixin(AdapterLitElement) {
 
     render() {
         const select2CSS = commonUtils.getAssetURL(select2CSSPath);
+        const i18n = this._i18n;
         return html`
             <link rel="stylesheet" href="${select2CSS}">
             <style>
diff --git a/packages/check-in-place-select/src/dbp-check-in-place-select-demo.js b/packages/check-in-place-select/src/dbp-check-in-place-select-demo.js
index 20c0b84e..d77e8561 100644
--- a/packages/check-in-place-select/src/dbp-check-in-place-select-demo.js
+++ b/packages/check-in-place-select/src/dbp-check-in-place-select-demo.js
@@ -1,4 +1,3 @@
-import {i18n} from './i18n.js';
 import {css, html} from 'lit-element';
 import {ScopedElementsMixin} from '@open-wc/scoped-elements';
 import {CheckInPlaceSelect} from './check-in-place-select.js';
@@ -34,10 +33,6 @@ export class CheckInPlaceSelectDemo extends ScopedElementsMixin(DBPLitElement) {
 
     connectedCallback() {
         super.connectedCallback();
-        i18n.changeLanguage(this.lang);
-
-        this.updateComplete.then(()=>{
-        });
     }
 
     static get styles() {
diff --git a/packages/check-in-place-select/src/i18n.js b/packages/check-in-place-select/src/i18n.js
index 498d9f03..fbc1b032 100644
--- a/packages/check-in-place-select/src/i18n.js
+++ b/packages/check-in-place-select/src/i18n.js
@@ -1,6 +1,10 @@
-import {createInstance} from '@dbp-toolkit/common/i18next.js';
+import {createInstance as _createInstance, setOverrides} 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');
+}
+
+export {setOverrides};
\ No newline at end of file
diff --git a/packages/check-in-place-select/src/i18n/de/translation.json b/packages/check-in-place-select/src/i18n/de/translation.json
index 5c44dae5..d1ff7885 100644
--- a/packages/check-in-place-select/src/i18n/de/translation.json
+++ b/packages/check-in-place-select/src/i18n/de/translation.json
@@ -1,7 +1,6 @@
 {
   "check-in-place-select": {
     "placeholder": "Z.B. \"P1\", \"Rechbauerstraße\", \"PZ2EG048\"",
-    "error-summary": "Ein Fehler ist aufgetreten",
     "login-required": "Anmeldung erforderlich"
   }
 }
diff --git a/packages/check-in-place-select/src/i18n/en/translation.json b/packages/check-in-place-select/src/i18n/en/translation.json
index 1a525f6b..40dc94b2 100644
--- a/packages/check-in-place-select/src/i18n/en/translation.json
+++ b/packages/check-in-place-select/src/i18n/en/translation.json
@@ -1,7 +1,6 @@
 {
   "check-in-place-select": {
     "placeholder": "E.g. \"P1\", \"Rechbauerstraße\", \"PZ2EG048\"",
-    "error-summary": "An error occurred",
     "login-required": "Login required"
   }
 }
-- 
GitLab