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

View File

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

View File

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

View File

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

View File

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

View File

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