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 {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(()=>{
});
......
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) => {
......
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
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 = {};
......
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