wxw
2025-05-12 c7c2f7aa20427204944ba80a2704232b2f281582
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var dictLanguage = {};
//初始化多语言
function initLanguage() {
  //console.log("initLanguage lang is " + $.cookie("lang"))
  registerWords();
}
 
function clearLangData(){
    sessionStorage.removeItem('enData')
}
    
function setLanguage() {
  // setCookie("lang=" + lang + "; path=/;");
  var lang = $.cookie("lang")
 //  if($.cookie("lang")=="en"){
    // setLanguage("en");
 //  }else if($.cookie("lang")=="zh"){
    // setLanguage("zh");
 //  }else{
    // setLanguage("zh");
 //  }
  translate(lang);
}
 
// function getCookieVal(name) {
//   var items = document.cookie.split(";");
//   for (var i in items) {
//     var cookie = $.trim(items[i]);
//     var eqIdx = cookie.indexOf("=");
//     var key = cookie.substring(0, eqIdx);
//     if (name == $.trim(key)) {
//       return $.trim(cookie.substring(eqIdx + 1));
//     }
//   }
//   return null;
// }
 
// function setCookie(cookie) {
//   var Days = 30; //此 cookie 将被保存 30 天
//   var exp = new Date(); //new Date("December 31, 9998");
//   exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
//   document.cookie = cookie+ ";expires=" + exp.toGMTString();
// }
 
function translate(lang) {
    //console.log("translate lang is "+lang)
  if(lang == "zh"){
      setZhLangVal()
      return
  }
  
  if(sessionStorage.getItem(lang + "Data") != null){
    dictLanguage = JSON.parse(sessionStorage.getItem(lang + "Data"));
    //console.log("111111111111111 dictLanguage "+JSON.stringify(dictLanguage))
  }else{
    loadDict();
  }
  setLangVal()
}
 
function setLangVal(){
  $("[lang]").each(function () {
    switch (this.tagName.toLowerCase()) {
      case "input":
        //console.log("33333333333333 "+$(this).attr("lang"))
        //console.log("placeholder "+$(this).attr("langholder"))
        $(this).val(__tr($(this).attr("lang")));
        $(this).attr("placeholder", __tr($(this).attr("langHolder")))
        break;
      default:
       //console.log("setLangVal "+$(this).attr("lang"))
        $(this).text(__tr($(this).attr("lang")));
    }
  });
}
 
function setZhLangVal(){
  $("[lang]").each(function () {
    switch (this.tagName.toLowerCase()) {
      case "input":
        //console.log("33333333333333 "+$(this).attr("lang"))
        //console.log("placeholder "+$(this).attr("langholder"))
        $(this).val($(this).attr("lang"));
        $(this).attr("placeholder", $(this).attr("langholder"))
        break;
      default:
       //console.log("setLangVal "+$(this).attr("lang"))
        $(this).text($(this).attr("lang"));
    }
  });
}
 
function __tr(src) {
  //console.log("src is "+src)
  return (dictLanguage[src] || src);
}
 
function loadDict() {
  var lang = $.cookie('lang')
  $.ajax({
    async: false,
    type: "GET",
    url: "lang/"+lang + ".json",
    success: function (msg) {
      dictLanguage = msg;
      sessionStorage.setItem(lang + 'Data', JSON.stringify(dictLanguage));
      //console.log("dictLanguage is "+JSON.stringify(dictLanguage))
      initLanguage()
    }
  });
 
}
 
// 遍历所有lang属性的标签赋值
function registerWords() {
    //console.log("2222222222222222222222222222")
  $("[lang]").each(function () {
    switch (this.tagName.toLowerCase()) {
      case "input":
        if($(this).attr("lang")==""){
          $(this).attr("lang", $(this).val());
          //console.log("text is "+$(this).val())
        }
        if($(this).attr("langholder") == ""){
            //console.log("2323 "+$(this).attr('placeholder'))
            $(this).attr("langholder", $(this).attr('placeholder'))
        }
        break;
      default:
        if($(this).attr("lang")==""){
          $(this).attr("lang", $(this).text());
        }
        //console.log("text22 is "+$(this).text())
    }
  });
}