绿色的风

春风所过之处,无不变绿!

博客园 首页 联系 订阅 管理

 我们下面分四大步骤来实现JS控制多国语显示

(1)创建配置文件【web.config】,并在文件写入以下信息

1 <!--版本大陆或台湾设置,0表示大陆,1表示台湾,2表示英文-->
2 <add key="Flag" value="0"/>

(2)创建全局文件【Global.asax】,并在文件里进行如下编码,Application_Start这个函数表示在应用程序启动的时候,先执行Application_Start函数里的内容,因此我们在这儿将配置文件里的版本设置信息转存入XML文件里,以方便用JS读取版本设置信息,我试过用JS不能读取“web.config”,所以先在应用程序加载的时候就把它转入到XML文件里,可能是我的知识还不到位,希望能起到抛砖引玉的效果。

代码
1 void Application_Start(object sender, EventArgs e)
2 {
3 WriteFlagToXML();
4 }
5 /// <summary>
6 /// 创建XML文件
7 /// </summary>
8 /// <param name="filePath">文件路径</param>
9   private void CreateXml(string filePath)
10 {
11 System.Xml.XmlDocument xmlDoc=new System.Xml.XmlDocument();
12 System.Xml.XmlDeclaration declaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8","yes");//文档声明部分
13   xmlDoc.AppendChild(declaration);
14
15 System.Xml.XmlElement rootNode = xmlDoc.CreateElement("configuration");//根节点
16   System.Xml.XmlElement xmlNode = xmlDoc.CreateElement("add");
17
18 System.Xml.XmlAttribute xmlKey = xmlDoc.CreateAttribute("key");
19 xmlKey.Value = " Flag";
20 xmlNode.Attributes.Append(xmlKey);
21
22 System.Xml.XmlAttribute xmlValue = xmlDoc.CreateAttribute("value");
23 xmlValue.Value = "0";
24 xmlNode.Attributes.Append(xmlValue);
25
26 string commentString = EventMessage.GetEventMessage(CommonString.XmlCommentString);// EventMessage是自定义类,从数据库中读取常量字符串,CommonString里面的常量字符串对应到数据库表中每一条记录的键
27 System.Xml.XmlComment comment = xmlDoc.CreateComment(commentString);
28 rootNode.AppendChild(comment);
29 rootNode.AppendChild(xmlNode);
30 xmlDoc.AppendChild(rootNode);
31
32 xmlDoc.Save(filePath);
33 }
34 /// <summary>
35 /// 将判断大陆版本,台湾版本,英文版本的标志写入xml文件 add by bai_clong
36 /// </summary>
37 private void WriteFlagToXML()
38 {
39 System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
40 string lastFilePath = System.AppDomain.CurrentDomain.BaseDirectory;
41 lastFilePath += "\\文件名.xml";
42 if (!System.IO.File.Exists(lastFilePath))//进行判断如果文件已经存在了,就不用再创建它,免得影响加载速度
43 {
44 CreateXml(lastFilePath);
45 }
46 xmldoc.Load(lastFilePath);
47 System.Xml.XmlNode xmlnode = xmldoc.SelectSingleNode("/configuration/add[@key=’Flag’]");
48 if(!xmlnode.Attributes["value"].Value.Equals(System.Configuration.ConfigurationManager.AppSettings["Flag"].ToString()))//如果xml文件中的版本设置信息跟配置文件中一样就没必要修改Flag的值
49 {
50 xmlnode.Attributes["value"].Value = System.Configuration.ConfigurationManager.AppSettings["Flag"].ToString();
51
52 xmldoc.Save(lastFilePath);
53 }
54
55 }
56

(3)编写JS代码来控制多国语显示

代码
1 // JScript File
2 var Flag="0";//2表示英文版本,1台湾版本,0表示大陆版本
3 var xmlDoc=null;//文档对象
4
5 //获取应用程序的根路径
6 function getRootPath(){…} //这段代码是拷贝别人的,因此不给大家展示了
7 //获得大陆或台湾简繁体标志信息
8 function getFlag ()
9 {
10 var configDoc=null;
11 // ie浏览器
12 if( window.ActiveXObject)
13 {
14 configDoc=new ActiveXObject("Microsoft.XMLDOM");//Msxml2.DOMDocument
15 }
16 // FireFox浏览器
17 else if(document.implementation && document.implementation.createDocument)
18 {
19 configDoc=document.implementation.createDocument("", "", null);
20 createSelectSingleNodeAddSelectNodesMethod();
21 }
22 configDoc.async=false;
23 configDoc.load(getRootPath()+"/文件名.xml");
24 var FlagNode=configDoc.selectSingleNode("configuration/add[@key='Flag']");
25 Flag= FlagNode.getAttribute("value");
26 }
27 //创建文文件对象
28 function createDocumentObject()
29 {
30 //ie浏览器
31 if(window.ActiveXObject)
32 {
33 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");//Msxml2.DOMDocument
34 }
35 //FireFox浏览器
36 else if(document.implementation && document.implementation.createDocument)
37 {
38 xmlDoc=document.implementation.createDocument("", "", null);
39 createSelectSingleNodeAddSelectNodesMethod();
40 }
41
42 xmlDoc.async=false;
43 }
44 //加载xml文件
45 function loadFile()
46 {
47 getFlag();//获得当前的版本设置信息
48 createDocumentObject();//创建文档对象
49 if(Flag =="2")
50 {
51 xmlDoc.load(getRootPath()+"/英文.xml");//加载英文版本
52 }
53 if(Flag =="1")
54 {
55 xmlDoc.load(getRootPath()+"/繁体.xml");//加载繁体版本
56 }
57 else if(Flag =="0")
58 {
59 xmlDoc.load(getRootPath()+"/简体.xml");//加载简体版本
60 }
61 }
62 //firefox并不支持selectSingleNode和selectNodes方法;下面两段是用XPath来解决firefox
63 //模拟selectSingleNode和selectNodes方法,网上COPY的
64 function createSelectSingleNodeAddSelectNodesMethod()
65 {
66 XMLDocument.prototype.selectSingleNode = Element.prototype.selectSingleNode = function (xpath){
67 var x = this .selectNodes(xpath)
68 if ( ! x || x.length < 1 ) return null ;
69 return x[ 0 ];
70 }
71 XMLDocument.prototype.selectNodes = Element.prototype.selectNodes = function (xpath){
72 var xpe = new XPathEvaluator();
73 var nsResolver = xpe.createNSResolver( this .ownerDocument == null ?
74 this .documentElement : this .ownerDocument.documentElement);
75 var result = xpe.evaluate(xpath, this , nsResolver, 0 , null );
76 var found = [];
77 var res;
78 while (res = result.iterateNext())
79 found.push(res);
80 return found;
81 }
82 }
83 //通过key获得对应的value值
84 function getMessage(message)
85 {
86 loadFile();//加载文件
87 //替换调字符串开头和结尾的空白符号
88 String.prototype.trim=function()
89 {
90 return this.replace(/(^\s*)|(\s*$)/g,"");
91 }
92
93 return xmlDoc.selectSingleNode("alertmessage/add[@key='"+message.trim()+"']").getAttribute("value");
94 }
95

****我的资源文件里的格式如下:

 

<?xml version="1.0" encoding="utf-8" ?>
<alertmessage>
<add key="NoFileToDelete" value="没有文件可删除!"></add>
//在此增加资源文件信息
</alertmessage>

 

 

 (4)上面一切都准备好了,现在开始引用;比如要在页面中弹出一消息框,我们可以这样实现

alert(getMessage("NoFileToDelete"));

运行结果!

请注意!上面的代码不一定运行能通过,因为部分代码我删除了。

我上面的办法可能比较笨,希望大家多多指教!

posted on 2010-04-25 10:14  bcl  阅读(2019)  评论(17编辑  收藏  举报