mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-22 03:51:23 +01:00
Updated Some Libraries
This commit is contained in:
parent
d44cca032e
commit
d2972c0871
11 changed files with 24 additions and 332 deletions
|
|
@ -1,55 +1 @@
|
|||
// Translation module
|
||||
"use strict";
|
||||
|
||||
void function() {
|
||||
window.lang = "en"; // default language
|
||||
|
||||
if (localStorage.getItem("lang")) window.lang = localStorage.getItem("lang");
|
||||
else {
|
||||
const isSupported = ln => ["ru"].includes(ln); // list of supported languages with at least 50% support
|
||||
const browserLang = navigator.language.split("-")[0];
|
||||
if (isSupported(browserLang)) window.lang = browserLang;
|
||||
}
|
||||
|
||||
selectLanguage.value = window.lang;
|
||||
if (window.lang === "en") return; // no need to translate
|
||||
initiateTranslation();
|
||||
|
||||
async function initiateTranslation() {
|
||||
const loaded = await loadTranslation();
|
||||
if (!loaded) {
|
||||
tip(`Cannot load ${window.lang} translation, check files in lang folder`, false, "error", 4000);
|
||||
window.lang == "en"; // set to default value
|
||||
return false;
|
||||
}
|
||||
|
||||
function loadTranslation() {
|
||||
return new Promise(resolve => {
|
||||
const script = document.createElement('script');
|
||||
script.src = `lang/lang-${window.lang}.js`
|
||||
document.head.append(script);
|
||||
script.onload = () => resolve(true);
|
||||
script.onerror = () => resolve(false);
|
||||
});
|
||||
}
|
||||
|
||||
if (translation["titleFull"]) document.title = translation["titleFull"];
|
||||
|
||||
void function translateDOM() {
|
||||
const tTags = Array.from(document.getElementsByTagName("t"));
|
||||
tTags.forEach(t => {
|
||||
const id = t.dataset.t;
|
||||
const text = translation[id];
|
||||
if (!text) return;
|
||||
t.innerHTML = text;
|
||||
});
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
}()
|
||||
|
||||
function translate(id, originalEn) {
|
||||
const text = translation[id];
|
||||
return text ? text : originalEn;
|
||||
}
|
||||
"use strict";function translate(n,t){const a=translation[n];return a||t}!function(){if(window.lang="en",localStorage.getItem("lang"))window.lang=localStorage.getItem("lang");else{const n=n=>["ru"].includes(n),t=navigator.language.split("-")[0];n(t)&&(window.lang=t)}selectLanguage.value=window.lang,"en"!==window.lang&&async function(){if(!await new Promise(n=>{const t=document.createElement("script");t.src=`lang/lang-${window.lang}.js`,document.head.append(t),t.onload=()=>n(!0),t.onerror=()=>n(!1)}))return tip(`Cannot load ${window.lang} translation, check files in lang folder`,!1,"error",4e3),window.lang,!1;translation.titleFull&&(document.title=translation.titleFull);Array.from(document.getElementsByTagName("t")).forEach(n=>{const t=n.dataset.t,a=translation[t];a&&(n.innerHTML=a)})}()}();
|
||||
Loading…
Add table
Add a link
Reference in a new issue