fleet-contractor/Site/js/language.js
2023-05-30 17:34:56 +08:00

126 lines
3.4 KiB
JavaScript

// multi-language
(function () {
'use strict';
function getKey(ele, attr) {
var key = ele.attr(attr).replace(/[.]/g, '_');
return key;
}
$.fn.applyFleetLanguageText = function (includeTitle) {
return this.each(function () {
var es = $(this).find('[data-lgid]');
var ele, key;
for (var i = 0; i < es.length; i++) {
ele = $(es[i]);
key = getKey(ele, 'data-lgid');
if (ele.is('input')) {
ele.val(GetTextByKey(key, ele.val()));
} else {
ele.text(GetTextByKey(key, ele.text()));
}
}
es = $(this).find('input[data-holder-lgid]');
for (var i = 0; i < es.length; i++) {
ele = $(es[i]);
key = getKey(ele, 'data-holder-lgid');
ele.attr('placeholder', GetTextByKey(key, ele.attr('placeholder')));
}
if (includeTitle) {
es = $(this).find('[data-title-lgid]');
for (var j = 0; j < es.length; j++) {
ele = $(es[j]);
key = getKey(ele, 'data-title-lgid');
ele.attr('title', GetTextByKey(key, key));
}
}
});
};
})();
var langObj = undefined;
function GetTextByKey(key, defaultValue) {
if (langObj == undefined) {
loadLanguage();
}
try {
if (langObj && langObj.Values) {
key = key.toUpperCase();
var val = langObj.Values[key];
if (val)
return val;
}
} catch (e) { }
return defaultValue;
}
function loadLanguage() {
var lang = _fleet.currentLang;
if (!lang)
lang = "en-us";
var ver = getFileVersion();
var langversion = window.localStorage[lang + "_v"];
if (!langversion || langversion == "" || eval(langversion) < ver) {
langObj = loadLanguageRes(ver);
window.localStorage[lang + "_v"] = ver;
window.localStorage[lang + "_res"] = JSON.stringify(langObj);
}
else {
var jsonstr = window.localStorage[lang + "_res"];
if (jsonstr && jsonstr != "")
langObj = JSON.parse(jsonstr);
if (!langObj) {
langObj = loadLanguageRes(ver);
window.localStorage[lang + "_res"] = JSON.stringify(langObj);
}
}
}
function getFileVersion() {
var ver = 0;
_network.commonpagequery(6, "", function (data) {
ver = data;
}, function (err) {
}, true, true);
return ver;
};
function loadLanguageRes(ver) {
if (langObj == undefined) {
var sp;
if (typeof _network.root === 'string') {
sp = _network.root;
} else if (typeof sitePath === 'string') {
sp = sitePath;
} else {
sp = '';
}
var lang = _fleet.currentLang;
if (!lang)
lang = "en-us";
try {
return loadJsonFromServer(sp + "Languages\\" + lang + ".json?s=" + ver);
}
catch {
return loadJsonFromServer(sp + "Languages\\en-us.json?s=" + ver);
}
}
}
function loadJsonFromServer(path) {//path 是json文件的地址
var result = null;
$.ajax({
url: path,
dataType: 'json',
type: 'GET',
async: false,
success: function (data) {
result = data;
}
});
return result;
};