From cff5ce2f55c6f27e784121c4f3a6a04acac2d668 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Thu, 1 Jul 2021 16:33:20 +0200
Subject: [PATCH] common: one i18next instance per element

---
 packages/common/dbp-common-demo.js |  7 ++++---
 packages/common/error.js           | 12 +++++++++---
 packages/common/i18n.js            |  6 ++++--
 packages/common/jsonld.js          |  9 +++++----
 4 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/packages/common/dbp-common-demo.js b/packages/common/dbp-common-demo.js
index 4e58093a..95a9986f 100644
--- a/packages/common/dbp-common-demo.js
+++ b/packages/common/dbp-common-demo.js
@@ -1,4 +1,4 @@
-import {i18n} from './i18n.js';
+import {createInstance} from './i18n.js';
 import {css, html, LitElement} from 'lit-element';
 import {ScopedElementsMixin} from '@open-wc/scoped-elements';
 import * as commonUtils from './utils.js';
@@ -8,7 +8,8 @@ import {getIconCSS, Icon, MiniSpinner, Button, LoadingButton, Spinner, InlineNot
 export class DbpCommonDemo extends ScopedElementsMixin(LitElement) {
     constructor() {
         super();
-        this.lang = 'de';
+        this._i18n = createInstance();
+        this.lang = this._i18n.language;
         this.noAuth = false;
     }
 
@@ -39,7 +40,7 @@ export class DbpCommonDemo extends ScopedElementsMixin(LitElement) {
 
     connectedCallback() {
         super.connectedCallback();
-        i18n.changeLanguage(this.lang);
+        this._i18n.changeLanguage(this.lang);
 
         this.updateComplete.then(()=>{
         });
diff --git a/packages/common/error.js b/packages/common/error.js
index aa61b9e2..303f3e86 100644
--- a/packages/common/error.js
+++ b/packages/common/error.js
@@ -1,5 +1,5 @@
 import {send as notify} from './notification';
-import {i18n} from "./i18n";
+import {createInstance} from "./i18n";
 
 /**
  * Escapes html
@@ -39,14 +39,17 @@ export const errorMixin = {
      * @param textStatus
      * @param errorThrown
      * @param icon
+     * @param lang
      */
-    handleXhrError(jqXHR, textStatus, errorThrown, icon = "sad") {
+    handleXhrError(jqXHR, textStatus, errorThrown, icon = "sad", lang = "de") {
         // return if user aborted the request
         if (textStatus === "abort") {
             return;
         }
 
         let body;
+        const i18n = createInstance();
+        i18n.changeLanguage(lang);
 
         if (jqXHR.responseJSON !== undefined && jqXHR.responseJSON["hydra:description"] !== undefined) {
             // response is a JSON-LD
@@ -86,14 +89,17 @@ export const errorMixin = {
      * @param error
      * @param summary
      * @param icon
+     * @param lang
      */
-    handleFetchError: async function (error, summary = "", icon = "sad") {
+    handleFetchError: async function (error, summary = "", icon = "sad", lang = "de") {
         // return if user aborted the request
         if (error.name === "AbortError") {
             return;
         }
 
         let body;
+        const i18n = createInstance();
+        i18n.changeLanguage(lang);
 
         try {
             await error.json().then((json) => {
diff --git a/packages/common/i18n.js b/packages/common/i18n.js
index dc0125f7..65d10ba7 100644
--- a/packages/common/i18n.js
+++ b/packages/common/i18n.js
@@ -1,6 +1,8 @@
-import {createInstance} from './i18next.js';
+import {createInstance as _createInstance} from './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/common/jsonld.js b/packages/common/jsonld.js
index 0a27bbfd..95cd28d7 100644
--- a/packages/common/jsonld.js
+++ b/packages/common/jsonld.js
@@ -1,6 +1,6 @@
 import {send as notify} from './notification';
 import * as utils from "./utils";
-import {i18n} from "./i18n";
+import {createInstance} from "./i18n";
 
 export default class JSONLD {
     constructor(baseApiUrl, entities) {
@@ -43,9 +43,7 @@ export default class JSONLD {
     }
 
     static _initialize(apiUrl, successFnc, failureFnc, lang = 'de') {
-        if (lang !== 'de') {
-            i18n.changeLanguage(lang);
-        }
+        JSONLD._i18n.changeLanguage(lang);
 
         // if init api call was already successfully finished execute the success function
         if (JSONLD.instances[apiUrl] !== undefined) {
@@ -72,6 +70,7 @@ export default class JSONLD {
 
     static _doInitialization(apiUrl) {
         const xhr = new XMLHttpRequest();
+        const i18n = JSON._i18n;
         xhr.open("GET", apiUrl, true);
 
         xhr.onreadystatechange = function () {
@@ -170,6 +169,7 @@ export default class JSONLD {
      * @param message
      */
     static _executeFailureFunctions(apiUrl, message = "") {
+        const i18n = JSON._i18n;
         if (JSONLD.failureFunctions[apiUrl] !== undefined) {
             for (const fnc of JSONLD.failureFunctions[apiUrl]) {
                 if (typeof fnc == 'function') {
@@ -297,6 +297,7 @@ export default class JSONLD {
     }
 }
 
+JSONLD._i18n = createInstance();
 JSONLD.instances = {};
 JSONLD.successFunctions = {};
 JSONLD.failureFunctions = {};
-- 
GitLab