feat: update locale to be more smart
This commit is contained in:
parent
4dd1cf209b
commit
ff01197ee6
5 changed files with 98 additions and 30 deletions
|
@ -1,10 +1,22 @@
|
|||
// [x-locale-key="<key>"]
|
||||
// html[x1-locale-file] -
|
||||
// html[x1-locale-watch] - watch document for changes and update
|
||||
// html[x0-locale-file="<url>"] - which file to use for locale
|
||||
// html[x0-locale-no-cache] - do not cache locale
|
||||
// html[x0-locale-watch] - watch document for changes and update
|
||||
// html[x0-locale-default="en"] - default "en"
|
||||
// html[x0-locale-prefer="en"] - prefer locale "en"
|
||||
(async () => {
|
||||
if (!document.querySelector("html").hasAttribute("x1-locale-file")) return;
|
||||
if (!document.querySelector("html").hasAttribute("x0-locale-file")) return;
|
||||
|
||||
const getLocale = async (url) => {
|
||||
let locale = (document.querySelector("html").hasAttribute("x0-locale-no-cache") ? undefined : localStorage.getItem(url)) ?? (await (await fetch(url)).text());
|
||||
if (!document.querySelector("html").hasAttribute("x0-locale-no-cache"))
|
||||
fetch(url).then(r => r.text().then(t => localStorage.setItem(url, t)));
|
||||
localStorage.setItem(url, locale);
|
||||
return JSON.parse(locale);
|
||||
};
|
||||
|
||||
let backupElements = window.x0?.registerStorage("locale") ?? {};
|
||||
let locale = (await (await fetch(document.querySelector("html").getAttribute("x1-locale-file"))).json()) ?? {};
|
||||
let locale = await getLocale(document.querySelector("html").getAttribute("x0-locale-file")) ?? {};
|
||||
|
||||
const getValueFromPath = (obj, path) => path.split(".").reduce((obj, key) => obj && obj[key], obj);
|
||||
|
||||
|
@ -13,8 +25,23 @@
|
|||
.forEach(e => {
|
||||
let uuid = (Math.random() + new Date().getTime()).toString(36);
|
||||
e.setAttribute("x1-locale-uuid", uuid);
|
||||
backupElements[uuid] = e.outerHTML;
|
||||
e.innerHTML = getValueFromPath(locale, e.getAttribute("x-locale-key"));
|
||||
backupElements[uuid] = e.innerHTML;
|
||||
let v = getValueFromPath(locale, e.getAttribute("x-locale-key"));
|
||||
if (typeof v === 'object') {
|
||||
[
|
||||
document.querySelector("html").getAttribute("x0-locale-prefer"),
|
||||
new URLSearchParams(window.location.search).get("locale"),
|
||||
navigator.userLanguage || navigator.language || navigator.browserLanguage,
|
||||
...navigator.languages,
|
||||
document.querySelector("html").getAttribute("x0-locale-default") ?? "en"
|
||||
]
|
||||
.filter(l => l)
|
||||
.map(l => l.toLowerCase())
|
||||
.forEach(l => v = v[l] ?? v)
|
||||
if (typeof v === 'object')
|
||||
v = '%%definition not found%%'
|
||||
}
|
||||
e.innerHTML = v;
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -26,9 +53,14 @@
|
|||
window.x0?.registerModuleUpdateHook("locale", updateLocale);
|
||||
|
||||
window.x0?.registerModuleRemoveHook("locale", (s) => {
|
||||
console.log(s);
|
||||
Object.keys(s)
|
||||
.forEach(uuid => {
|
||||
document.querySelector(`[x1-locale-uuid="${uuid}"]`).outerHTML = s[uuid];
|
||||
let el = document.querySelector(`[x1-locale-uuid="${uuid}"]`);
|
||||
requestAnimationFrame(() => {
|
||||
el.removeAttribute("x1-locale-uuid");
|
||||
el.innerHTML = s[uuid];
|
||||
})
|
||||
})
|
||||
})
|
||||
})()
|
Loading…
Add table
Add a link
Reference in a new issue