javascript国际化解决方案
-----》中文国际化资源文件MessageResource_zh_CN.js
var resource={ hello :"你好,世界!", user:"小明" };
-----》英文国际化资源文件MessageResource_en_US.js
var resource={ hello :"HELLO,WORLD!", user:"xiao ming" };
-----》创建国际化文件调用方法
insertJS(); function getCookie(objName) {// 获取指定名称的cookie的值 var arrStr = document.cookie.split("; "); for (var i = 0; i < arrStr.length; i++) { var temp = arrStr[i].split("="); if (temp[0] == objName) { return unescape(temp[1]); } } return ""; } function insertJS() { var language = getCookie("language"); if (language.indexOf("en_US") > -1) { document.writeln('<script type="text/javascript" src="MessageResource_en_US.js"></script>'); } else if (language.indexOf("zh_CN") > -1 || language == "") { document.writeln('<script type="text/javascript" src="MessageResource_zh_CN.js"></script>'); } } function getInternationalizationJs(key) { return resource[key]; }
-----》设置Cookie
Ext.onReady(function(){ Ext.get(btn1).on('click',function(){ alert(getInternationalizationJs("user"));//直接调用方法。 }) Ext.get("btnzh").on('click',setCookie) Ext.get("btnen").on('click',setCookie) }); function setCookie(){ switch(this.id){ case("btnzh"): document.cookie = encodeURI("language") + "=" +encodeURI("zh_CN") + ";expires=" + new Date(new Date().getTime()+(1000*60*60*24*30)).toUTCString(); break; case("btnen"): document.cookie = encodeURI("language") + "=" +encodeURI("en_US") + ";expires=" + new Date(new Date().getTime()+(1000*60*60*24*30)).toUTCString(); break; } window.location.reload(); //页面刷新 }
-----》html文件
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>js国际化测试程序</title> <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" /> <!-- GC --> <!-- LIBS --> <script type="text/javascript" src="../adapter/ext/ext-base.js"></script> <!-- ENDLIBS --> <script type="text/javascript" src="../ext-all.js"></script> <script type="text/javascript" src="i18n.js"></script> <script type="text/javascript" src="temp.js"></script> </head> <body> <input type="button" id="btn1" value="test"> <br> <input type="button" id="btnzh" value="中文设置"> <br> <input type="button" id="btnen" value="英文设置"> <br> <div id="mydiv">xxxx</div> </body> </html>
i18n.js是参考的网上的代码,具体地址找不到了,见谅没有写出引用地址。