2019-04-11 10:47:58 -04:00
|
|
|
/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2020-03-16 08:53:51 -04:00
|
|
|
let gs_kiri_lang = exports;
|
2019-04-11 10:47:58 -04:00
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
|
|
if (!self.kiri) self.kiri = {};
|
|
|
|
|
if (self.kiri.lang) return;
|
|
|
|
|
|
2020-03-16 08:53:51 -04:00
|
|
|
let KIRI = self.kiri,
|
2020-03-18 14:09:38 -04:00
|
|
|
LANG = self.kiri.lang = { current: {} },
|
2020-03-16 08:53:51 -04:00
|
|
|
lset = navigator.language.toLocaleLowerCase();
|
|
|
|
|
|
2020-03-18 14:09:38 -04:00
|
|
|
LANG.get = function() {
|
|
|
|
|
return lset;
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-16 08:53:51 -04:00
|
|
|
LANG.set = function() {
|
|
|
|
|
let map, key, keys = [...arguments];
|
2020-03-18 14:09:38 -04:00
|
|
|
// provide default if none given
|
|
|
|
|
if (keys.length === 0) {
|
2020-03-20 16:06:33 -04:00
|
|
|
keys = [lset, lset.split('-')[0], 'en'];
|
2020-03-18 14:09:38 -04:00
|
|
|
}
|
2020-03-16 08:53:51 -04:00
|
|
|
for (let i=0; i<keys.length; i++)
|
|
|
|
|
{
|
|
|
|
|
key = keys[i]
|
|
|
|
|
if (!(map = LANG[key])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-03-18 14:09:38 -04:00
|
|
|
lset = key;
|
2019-04-12 22:06:53 -04:00
|
|
|
let missing = [];
|
|
|
|
|
let invalid = [];
|
2019-04-11 10:47:58 -04:00
|
|
|
// default to EN values when missing
|
|
|
|
|
Object.keys(LANG.en).forEach(key => {
|
|
|
|
|
if (!map[key]) {
|
|
|
|
|
map[key] = LANG.en[key];
|
2019-04-12 22:06:53 -04:00
|
|
|
missing.push(key);
|
2019-04-11 10:47:58 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// update current map from chosen map
|
|
|
|
|
Object.keys(map).forEach(key => {
|
2019-04-12 22:06:53 -04:00
|
|
|
if (LANG.en[key]) {
|
|
|
|
|
LANG.current[key] = map[key];
|
|
|
|
|
} else {
|
|
|
|
|
invalid.push(key);
|
|
|
|
|
}
|
2019-04-11 10:47:58 -04:00
|
|
|
});
|
2019-04-12 22:06:53 -04:00
|
|
|
if (missing.length) {
|
|
|
|
|
console.log(`language map "${key}" missing keys [${missing.length}]: ${missing.join(', ')}`);
|
|
|
|
|
}
|
|
|
|
|
if (invalid.length) {
|
|
|
|
|
console.log(`language map "${key}" invalid keys [${invalid.length}]: ${invalid.join(', ')}`);
|
|
|
|
|
}
|
2020-03-16 08:53:51 -04:00
|
|
|
return key;
|
2019-04-11 10:47:58 -04:00
|
|
|
}
|
2020-03-16 08:53:51 -04:00
|
|
|
return undefined;
|
2019-04-11 10:47:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})();
|