fix: intl for ESS
This commit is contained in:
@ -6,27 +6,27 @@ class JsI18n {
|
||||
constructor() {
|
||||
this.locale = ""; //Current locale
|
||||
this.locales = new Array(); //Available locales
|
||||
this.overwrites = new Array();
|
||||
this.defaultLocale = "fr";
|
||||
}
|
||||
|
||||
/*
|
||||
Method for automatically detecting the language, does not work in every browser.
|
||||
*/
|
||||
detectLanguage() {
|
||||
async detectLanguage() {
|
||||
const customLangs = document.querySelectorAll('hubl-lang');
|
||||
if(customLangs) {
|
||||
for(let lang of customLangs) {
|
||||
let name = lang.getAttribute('lang'),
|
||||
file = lang.getAttribute('file');
|
||||
if(window.hubl.intl.locales[name.toString()] == undefined) {
|
||||
return fetch(file).then((result) => {
|
||||
if (result.ok) {
|
||||
result.json().then(e => {
|
||||
window.hubl.intl.addLocale(name, e);
|
||||
});
|
||||
}
|
||||
window.hubl.intl.resumeDetection();
|
||||
});
|
||||
if (customLangs) {
|
||||
for (let lang of customLangs) {
|
||||
let name = lang.getAttribute('lang'),
|
||||
file = lang.getAttribute('file');
|
||||
let result = await fetch(file);
|
||||
if (result.ok) {
|
||||
let json = await result.json();
|
||||
if(this.overwrites[name.toString()] != undefined) {
|
||||
this.mergeDeep(this.overwrites[name.toString()], json);
|
||||
} else {
|
||||
this.overwrites[name.toString()] = json;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -35,10 +35,10 @@ class JsI18n {
|
||||
|
||||
resumeDetection() {
|
||||
const langComponent = document.querySelector('hubl-fallback-lang');
|
||||
if(langComponent) {
|
||||
if(langComponent.hasAttribute('lang')) {
|
||||
if (langComponent) {
|
||||
if (langComponent.hasAttribute('lang')) {
|
||||
this.defaultLocale = langComponent.getAttribute('lang');
|
||||
if(langComponent.hasAttribute('force')) {
|
||||
if (langComponent.hasAttribute('force')) {
|
||||
localStorage.setItem('language', this.defaultLocale);
|
||||
}
|
||||
}
|
||||
@ -52,6 +52,30 @@ class JsI18n {
|
||||
}
|
||||
};
|
||||
|
||||
isObject(item) {
|
||||
return (item && typeof item === 'object' && !Array.isArray(item));
|
||||
}
|
||||
|
||||
mergeDeep(target, ...sources) {
|
||||
if (!sources.length) return target;
|
||||
const source = sources.shift();
|
||||
if (this.isObject(target) && this.isObject(source)) {
|
||||
for (const key in source) {
|
||||
if (this.isObject(source[key])) {
|
||||
if (!target[key]) Object.assign(target, {
|
||||
[key]: {}
|
||||
});
|
||||
this.mergeDeep(target[key], source[key]);
|
||||
} else {
|
||||
Object.assign(target, {
|
||||
[key]: source[key]
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.mergeDeep(target, ...sources);
|
||||
}
|
||||
|
||||
/*
|
||||
Translates tag contents and
|
||||
attributes depending on the
|
||||
@ -84,7 +108,7 @@ class JsI18n {
|
||||
if (placeholder != null) {
|
||||
this.translateNodeContent(placeholder.attributes['placeholder'], k);
|
||||
let input = node.querySelector('[name="' + attr.replace("placeholder-", "") + '"] > input');
|
||||
if(input != null) {
|
||||
if (input != null) {
|
||||
this.translateNodeContent(input.attributes['placeholder'], k);
|
||||
}
|
||||
}
|
||||
@ -145,21 +169,20 @@ class JsI18n {
|
||||
if the locale is already defined.
|
||||
*/
|
||||
addLocale(locale, translations) {
|
||||
if (this.overwrites[locale.toString()] != undefined) {
|
||||
this.mergeDeep(translations, this.overwrites[locale.toString()]);
|
||||
}
|
||||
this.locales[locale.toString()] = translations;
|
||||
}
|
||||
|
||||
fetchLocale(locale) {
|
||||
if(this.locales[locale.toString()] == undefined) {
|
||||
return fetch(`/locales/${locale}.json`).then((result) => {
|
||||
if (result.ok) {
|
||||
result.json().then(e => {
|
||||
this.addLocale(locale, e);
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return (new Promise()).resolve();
|
||||
}
|
||||
return fetch(`/locales/${locale}.json`).then((result) => {
|
||||
if (result.ok) {
|
||||
result.json().then(e => {
|
||||
this.addLocale(locale, e);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
@ -168,7 +191,7 @@ class JsI18n {
|
||||
setLocale(locale) {
|
||||
try {
|
||||
this.fetchLocale(locale).then(() => {
|
||||
if(this.locale) {
|
||||
if (this.locale) {
|
||||
localStorage.setItem('language', this.locale);
|
||||
}
|
||||
this.processPage();
|
||||
@ -190,11 +213,11 @@ class JsI18n {
|
||||
t(key) {
|
||||
var translations = this.locales[this.locale];
|
||||
if (translations == undefined) {
|
||||
if(this.locales.length > 1) {
|
||||
if (this.locales.length > 1) {
|
||||
translations = this.locales[this.defaultLocale];
|
||||
}
|
||||
}
|
||||
if(translations != undefined) {
|
||||
if (translations != undefined) {
|
||||
let translation = key.toString().split('.').reduce((o, i) => (o ? o[i] : undefined), translations);
|
||||
if (typeof translation == "string") {
|
||||
return translation;
|
||||
|
Reference in New Issue
Block a user