fix: intl for ESS

This commit is contained in:
Jean-Baptiste Pasquier 2021-04-08 20:22:39 +02:00
parent a5ede42a91
commit e2c9da2459
7 changed files with 1289 additions and 752 deletions

1902
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -15,6 +15,9 @@
"cypress:info": "cypress info",
"test": "cypress run"
},
"browserslist": [
"last 2 Chrome versions"
],
"release": {
"branches": [
"master"
@ -49,7 +52,7 @@
"cross-env": "^7.0.3",
"fs-extra": "^9.0.1",
"normalize.css": "^8.0.1",
"parcel-bundler": "^1.12.4",
"parcel-bundler": "^1.12.5",
"pug": "^3.0.0",
"rimraf": "^2.7.1",
"sass": "^1.29.0"

View File

@ -32,13 +32,7 @@
"circlesBrowse": "Browse circles",
"circleCreate": "Create circle",
"messages": "Messages",
"search": "Search",
"republiqueESS": {
"home": "Home",
"contribute": "Contribute",
"thematics": "Thematics",
"community": "Community"
}
"search": "Search"
},
"about": {
"title": "About",
@ -131,10 +125,7 @@
"subTitle": "Members :"
},
"extensions": {
"associated": "Associated circle",
"republiqueESS": {
"associatedThematic": "Associated thematic"
}
"associated": "Associated circle"
}
},
"communities": {

View File

@ -32,13 +32,7 @@
"circlesBrowse": "Examinar los círculos",
"circleCreate": "Crea un círculo.",
"messages": "Mensajes",
"search": "Buscar",
"republiqueESS": {
"home": "Hogar",
"contribute": "Contribuir",
"thematics": "Temáticas",
"community": "Comunidad"
}
"search": "Buscar"
},
"about": {
"title": "Acerca de",
@ -131,10 +125,7 @@
"subTitle": "Miembrxs: "
},
"extensions": {
"associated": "Círculo asociado",
"republiqueESS": {
"associatedThematic": "Temática asociada"
}
"associated": "Círculo asociado"
}
},
"communities": {

View File

@ -9,9 +9,7 @@
},
"menuLeft": {
"emptyCircleProjectContact": {
"empty": "Il n'y a aucun résultat dans cette section.",
"project": "projet",
"circle": "cercle"
"empty": "Il n'y a aucun résultat dans cette section."
},
"contact": {
"create": "Retrouve tes contacts sur",
@ -32,13 +30,7 @@
"circlesBrowse": "Parcourir les cercles",
"circleCreate": "Créer un cercle",
"messages": "Messages",
"search": "Rechercher",
"republiqueESS": {
"home": "Accueil",
"contribute": "Contribuer",
"thematics": "Thématiques",
"community": "Communauté"
}
"search": "Rechercher"
},
"about": {
"title": "A propos",
@ -131,10 +123,7 @@
"subTitle": "Membres :"
},
"extensions": {
"associated": "Cercle associé",
"republiqueESS": {
"associatedThematic": "Thématique associée"
}
"associated": "Cercle associé"
}
},
"communities": {

View File

@ -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;

View File

@ -131,10 +131,10 @@ nav#main__menu {
.menu-admin {
position: absolute;
z-index: 3016;
left: 78px;
left: 15%;
top: 36px;
text-align: end;
width: 64%;
width: 80%;
@media (max-width: 768px) {
width: 100%;