Files
apt-nl-map/static/Magic4/js/i18n/i18n.js
2024-12-03 16:40:54 +08:00

162 lines
4.8 KiB
JavaScript

var LANGUAGE_Index = {name:"en_GB"}; //标识语言
var enGbMap = new Map();
var zhGbMap = new Map();
jQuery(document).ready(function () {
LANGUAGE_Index.name = jQuery.i18n.browserLang({}); //获取浏览器的语言 zh-CN
console.log('浏览器默认语言是:'+ LANGUAGE_Index.name)
// if (LANGUAGE_Index.name == "zh-CN" ) {
// LANGUAGE_Index.name = "zh_CN"
// $("#i18nChange span").text("中文");
// zhGbMap = loadProperties(LANGUAGE_Index.name,new Map());
// $("#sidebar ul li:visible").get(0).click();
// }
// if (LANGUAGE_Index.name == "en" || LANGUAGE_Index.name == "en-US") {
// LANGUAGE_Index.name = "en_GB"
// $("#i18nChange span").text("English");
// enGbMap = loadProperties(LANGUAGE_Index.name, new Map());
// $("#sidebar ul li:visible").get(0).click();
// }
var type = localStorage.getItem("locale");
if (type == "zh-cn" ) {
LANGUAGE_Index.name = "zh_CN";
$("#i18nChange span").text("中文");
zhGbMap = loadProperties(LANGUAGE_Index.name,new Map());
}else if (type == "en-us") {
LANGUAGE_Index.name = "en_GB";
$("#i18nChange span").text("English");
enGbMap = loadProperties(LANGUAGE_Index.name, new Map());
}else {
LANGUAGE_Index.name = "en_GB";
$("#i18nChange span").text("English");
enGbMap = loadProperties(LANGUAGE_Index.name, new Map());
}
});
$("#i18nDropdown").on("click",".i18nChange",function (){
let txt = $("#i18nChange span").text();
let txt1 = $(this).find("span").text();
if(txt == txt1){
return;
}
$("#i18nChange span").text(txt1);
let map = new Map();
let val = $(this).find("span").attr('data-i18n-change');
if (val == "zh_CN" ) {
if(zhGbMap.size < 1){
zhGbMap = loadProperties(val , new Map());
LANGUAGE_Index.name = val;
return;
}
map = zhGbMap;
}
if (val == "en_GB") {
if(enGbMap.size < 1){
enGbMap = loadProperties(val , new Map());
LANGUAGE_Index.name = val;
return;
}
map = enGbMap;
}
loadProperties(val , map);
LANGUAGE_Index.name = val;
})
function changeI18n(val){
var type = "";
if (val == "zh-cn" ) {
type = "zh_CN";
if(zhGbMap.size < 1){
zhGbMap = loadProperties(type, new Map());
LANGUAGE_Index.name = type;
return;
}
map = zhGbMap;
}
if (val == "en-us") {
type = "en_GB";
if(enGbMap.size < 1){
enGbMap = loadProperties(type, new Map());
LANGUAGE_Index.name = type;
return;
}
map = enGbMap;
}
loadProperties(type , map);
LANGUAGE_Index.name = type;
}
// function selectOnchang(obj){
//
// let map = new Map();
// let val = obj.options[obj.selectedIndex].value
// if (val == "zh_CN" ) {
// if(zhGbMap.size < 1){
// zhGbMap = loadProperties(val , new Map());
// LANGUAGE_Index.name = val;
// return;
// }
// map = zhGbMap;
// }
// if (val == "en_GB") {
// if(enGbMap.size < 1){
// enGbMap = loadProperties(val , new Map());
// LANGUAGE_Index.name = val;
// return;
// }
// map = enGbMap;
// }
// loadProperties(val , map);
// LANGUAGE_Index.name = val;
// }
window.loadProperties = function(type,smap) {
let map = new Map();
if(smap.size < 1){
jQuery.i18n.properties({
name: 'strings', // 资源文件名称
path: 'js/i18n/', // 资源文件所在目录路径
mode: 'both', // 模式:变量或 Map
language: type, // 对应的语言
cache: false,
encoding: 'UTF-8',
callback: function () { // 回调方法;
smap = new Map(Object.entries($.i18n.map));
}
});
}
map = smap;
try {
//初始化页面元素
$('[data-i18n-placeholder]').each(function () {
$(this).attr('placeholder', map.get($(this).data('i18n-placeholder')));
});
$('[data-i18n-text]').each(function () {
//如果text里面还有html需要过滤掉
var html = $(this).html();
var reg = /<(.*)>/;
if (reg.test(html)) {
var htmlValue = reg.exec(html)[0];
$(this).html(htmlValue + map.get($(this).data('i18n-text')));
}
else {
$(this).text(map.get($(this).data('i18n-text')));
}
});
$('[data-i18n-value]').each(function () {
$(this).val(map.get($(this).data('i18n-value')));
});
}
catch(ex){ }
$.cookie("LanguageIndex1",type);
sessionStorage.setItem("LanguageMap",JSON.stringify(Object.fromEntries(map)));
return map;
}