forked from omsi6/omsi6.github.io
-
Notifications
You must be signed in to change notification settings - Fork 31
/
localization.js
98 lines (92 loc) · 3.58 KB
/
localization.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
window.Localization = {
// config
// set to true for more console.log
debug: false,
defaultLang: "en-EN",
supportedLang: {
"en-EN": "English",
//"fr-FR": "Français",
},
// key used in the get parameter of the URL to set a specific language
getKey: "lg",
// html selector of the div to put the localization menu in
handle: "#localization_menu",
// vars
currentLang: null,
libs: {},
lastLib: null,
// ====== PUBLIC ======
// starts up the module
init() {
Localization.currentLang = Localization.getUrlVars()[Localization.getKey];
if (typeof(Localization.currentLang) === "undefined")
Localization.currentLang = Localization.defaultLang;
},
// to load a specific lib and have an optional callback
loadLib(libName, callback) {
Localization.loadXML(libName, function(xmlData) {
Localization.saveLib(libName, xmlData);
if (typeof(callback) !== "undefined") callback.call(this);
});
},
// lib can be ignored to use the last used lib. returns the text for the given key
txt(path, lib) {
// eslint-disable-next-line no-param-reassign
if (typeof(lib) === "undefined") lib = "game";
const libObject = $(Localization.libs[lib]);
let txt;
if (libObject.length) txt = $(Localization.libs[lib]).find(path).text();
if (txt === "") {
console.warn(`Missing text in lang '${Localization.currentLang}' for key ${path} in lib ${lib}`);
txt = $(Localization.libs.fallback).find(path).text();
if (txt === "") {
console.warn(`Missing fallback for key ${path}`);
txt = `[${path}]`;
}
}
return txt;
},
// lib can be ignored to use the last used lib. returns the texts for the given key as objects
txtsObj(path, lib) {
if (typeof(lib) === "undefined") return $(Localization.libs[Localization.lastLib]).find(path);
return $(Localization.libs[lib]).find(path);
},
// will update every dom element using the .localized class, with a valid js-data "lockey"
localizePage(lib) {
$(".localized").each((_index, localizedElement) => {
$(localizedElement).html(Localization.txt($(localizedElement).data("lockey"), lib));
});
},
// ====== PRIVATE ======
saveLib(libName, xmlData) {
if (Localization.debug)
console.log(`Loaded lib ${libName} : `, xmlData);
Localization.libs[libName] = xmlData;
Localization.lastLib = Localization.lastLib === null ? libName : Localization.lastLib;
},
// function triggered by the localization menu
change() {
const vars = Localization.getUrlVars();
vars.lg = $(Localization.handle).val();
window.location.href = `${window.location.origin + window.location.pathname}?${$.param(vars)}`;
},
loadXML(libName, callback) {
if (libName === "fallback") $.get("lang/en-EN/game.xml", null, callback, "xml");
else $.get(`lang/${Localization.currentLang}/${libName}.xml`, null, callback, "xml");
},
getUrlVars() {
const vars = {};
parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/giu, (_m, key, value) => {
vars[key] = value;
});
return vars;
},
};
Localization.init();
// binding the _txt function for simplier use
window._txt = Localization.txt;
window._txtsObj = Localization.txtsObj;
let locCheck = false;
Localization.loadLib("fallback", () => {
Localization.loadLib("game", () => locCheck = true);
});