grid-apps-cmms/js/kiri-lang.js

61 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-04-11 10:47:58 -04:00
/** Copyright 2014-2019 Stewart Allen -- All Rights Reserved */
"use strict";
let gs_kiri_lang = exports;
2019-04-11 10:47:58 -04:00
(function() {
if (!self.kiri) self.kiri = {};
if (self.kiri.lang) return;
let KIRI = self.kiri,
2020-03-18 14:09:38 -04:00
LANG = self.kiri.lang = { current: {} },
lset = navigator.language.toLocaleLowerCase();
2020-03-18 14:09:38 -04:00
LANG.get = function() {
return lset;
};
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) {
keys = [lset, lset.split('-')[0], 'en'];
2020-03-18 14:09:38 -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;
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];
missing.push(key);
2019-04-11 10:47:58 -04:00
}
});
// update current map from chosen map
Object.keys(map).forEach(key => {
if (LANG.en[key]) {
LANG.current[key] = map[key];
} else {
invalid.push(key);
}
2019-04-11 10:47:58 -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(', ')}`);
}
return key;
2019-04-11 10:47:58 -04:00
}
return undefined;
2019-04-11 10:47:58 -04:00
}
})();