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

common: one i18next instance per element

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