132 lines
4.8 KiB
JavaScript
132 lines
4.8 KiB
JavaScript
(function () {
|
|
var LANGS = ["en", "zh-CN", "zh-TW", "kk"];
|
|
|
|
function pick(tag) {
|
|
var text = String(tag || "").toLowerCase().replace(/_/g, "-");
|
|
if (text === "zh-tw" || text === "zh-hk" || text === "zh-mo" || text === "zh-hant") return "zh-TW";
|
|
if (text === "zh" || text === "zh-cn" || text === "zh-sg" || text === "zh-hans" || text.indexOf("zh-") === 0) return "zh-CN";
|
|
if (text === "kk" || text.indexOf("kk-") === 0) return "kk";
|
|
if (text === "en" || text.indexOf("en-") === 0) return "en";
|
|
return "";
|
|
}
|
|
|
|
function chosen() {
|
|
try {
|
|
var saved = localStorage.getItem("mifipanel.lang");
|
|
if (LANGS.indexOf(saved) >= 0) return saved;
|
|
} catch (err) {}
|
|
var list = navigator.languages && navigator.languages.length ? navigator.languages : [navigator.language || "en"];
|
|
for (var i = 0; i < list.length; i++) {
|
|
var hit = pick(list[i]);
|
|
if (hit) return hit;
|
|
}
|
|
return "en";
|
|
}
|
|
|
|
function fetchJson(lang) {
|
|
try {
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.open("GET", "/i18n/locales/" + lang + ".json", false);
|
|
xhr.send(null);
|
|
if (xhr.status >= 200 && xhr.status < 300 && xhr.responseText) return JSON.parse(xhr.responseText);
|
|
} catch (err) {}
|
|
return null;
|
|
}
|
|
|
|
function lookup(tree, key) {
|
|
if (!tree) return null;
|
|
var node = tree;
|
|
var parts = String(key).split(".");
|
|
for (var i = 0; i < parts.length; i++) {
|
|
if (node == null || typeof node !== "object") return null;
|
|
node = node[parts[i]];
|
|
}
|
|
return typeof node === "string" ? node : null;
|
|
}
|
|
|
|
function fill(text, vars) {
|
|
if (!vars) return text;
|
|
return text.replace(/\{(\w+)\}/g, function (_, name) {
|
|
return vars[name] == null ? "" : String(vars[name]);
|
|
});
|
|
}
|
|
|
|
var requested = chosen();
|
|
var loaded = requested === "en" ? null : fetchJson(requested);
|
|
var lang = loaded ? requested : "en";
|
|
var english = fetchJson("en") || {};
|
|
var pack = lang === "en" ? english : loaded;
|
|
|
|
function translate(key, vars) {
|
|
var value = lookup(pack, key);
|
|
if (value == null) value = lookup(english, key);
|
|
if (value == null) return null;
|
|
return fill(value, vars);
|
|
}
|
|
|
|
function text(message) {
|
|
if (!message) return "";
|
|
var key = message;
|
|
var extra = "";
|
|
var bar = message.indexOf("|");
|
|
if (bar > 0) {
|
|
key = message.slice(0, bar);
|
|
extra = message.slice(bar + 1);
|
|
}
|
|
var value = translate(key, { n: extra, detail: extra });
|
|
return value == null ? message : value;
|
|
}
|
|
|
|
function paint(el) {
|
|
if (!el || !el.getAttribute) return;
|
|
if (el.hasAttribute("data-i18n")) {
|
|
var value = translate(el.getAttribute("data-i18n"));
|
|
if (value != null) el.textContent = value;
|
|
}
|
|
if (el.hasAttribute("data-i18n-placeholder")) {
|
|
var placeholder = translate(el.getAttribute("data-i18n-placeholder"));
|
|
if (placeholder != null) el.setAttribute("placeholder", placeholder);
|
|
}
|
|
if (el.hasAttribute("data-i18n-title")) {
|
|
var title = translate(el.getAttribute("data-i18n-title"));
|
|
if (title != null) el.setAttribute("title", title);
|
|
}
|
|
if (el.hasAttribute("data-i18n-value")) {
|
|
var input = translate(el.getAttribute("data-i18n-value"));
|
|
if (input != null) el.value = input;
|
|
}
|
|
if (el.hasAttribute("data-i18n-req")) {
|
|
var required = translate(el.getAttribute("data-i18n-req"));
|
|
if (required != null) el.setAttribute("lay-reqtext", required);
|
|
}
|
|
}
|
|
|
|
function apply(root) {
|
|
var scope = root || document;
|
|
if (scope.querySelectorAll) {
|
|
scope.querySelectorAll("[data-i18n],[data-i18n-placeholder],[data-i18n-title],[data-i18n-value],[data-i18n-req]").forEach(paint);
|
|
scope.querySelectorAll("[data-title]").forEach(function (el) {
|
|
var value = translate(el.getAttribute("data-title"));
|
|
if (value != null) el.setAttribute("data-title", value);
|
|
});
|
|
}
|
|
paint(scope);
|
|
if (document.documentElement) document.documentElement.lang = lang;
|
|
}
|
|
|
|
window.I18n = {
|
|
lang: lang,
|
|
langs: LANGS,
|
|
t: function (key, vars) {
|
|
var value = translate(key, vars);
|
|
return value == null ? key : value;
|
|
},
|
|
text: text,
|
|
has: function (key) { return translate(key) != null; },
|
|
apply: apply
|
|
};
|
|
|
|
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", function () { apply(document); });
|
|
else apply(document);
|
|
})();
|