XML文档:(注意:格式必须正确,可在浏览器中输入路径进行检查。)
<?xml version="1.0" encoding="utf-8" ?>
<tran>
<loginname>
   <en>loginName</en>
   <zh-cn>用户名</zh-cn>
</loginname>
<password>
   <en>Password</en>
   <zh-cn>密码</zh-cn>
</password>
</tran>



HTML代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title></title>
  <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
  <meta name="ProgId" content="VisualStudio.HTML">
  <meta name="Originator" content="Microsoft Visual Studio .NET 7.1">
  <script language="javascript">
 
//得到xml上下文
function getXMLHandler(xmlFile){
  var xmlDoc;
  if(window.ActiveXObject){
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.onreadystatechange = function() {
      if(xmlDoc.readyState == 4);// doAction();
    }
    xmlDoc.load(xmlFile);
  }
  else if(document.implementation&&document.implementation.createDocument){
    xmlDoc=document.implementation.createDocument('','',null);
    xmlDoc.onload=doAction();
    xmlDoc.load(xmlFile);
  }
  else
    return null;
  return xmlDoc;
}

//得到节点值
//xmlFile:XML文件
//language:语言
//tagName:元素名
function getNodeValue(xmlFile,language,tagName){
  var xmlDoc = getXMLHandler(xmlFile);
  //alert(xmlDoc.getElementsByTagName("loginName")[0].childNodes[1].firstChild.nodeValue);
  //alert(xmlDoc.getElementsByTagName("tran")[0].childNodes[1].nodeName);
  //alert(xmlDoc.getElementsByTagName("tran")[0].childNodes.length);
  if( xmlDoc.getElementsByTagName(tagName) != null)
  if(language == "en")
  {
     return xmlDoc.getElementsByTagName(tagName)[0].childNodes[0].firstChild.nodeValue;
  }
  else
  {
     return xmlDoc.getElementsByTagName(tagName)[0].childNodes[1].firstChild.nodeValue;
  }
 
}

//转变元素语言
//targetDocument:文档,tag:标签名,propertyToSet:方式,language:语言
function TranslateElements( targetDocument, tag, propertyToSet, language )
{
        var e = targetDocument.getElementsByTagName(tag) ;
        for ( var i = 0 ; i < e.length ; i++ )
        {
                var sKey = e[i].getAttribute('id') ;
               
                if ( sKey )
                {
                        var s = getNodeValue('checkName.xml',language,(sKey.substring(1,sKey.length)).toLowerCase());
                        if ( s )
                                eval( 'e[i].' + propertyToSet + ' = s' ) ;
                       
                }
        }
}

function TranslatePage( language )
{
        //this.TranslateElements( targetDocument, 'INPUT', 'value' ) ;
        //this.TranslateElements( targetDocument, 'SPAN', 'innerHTML' ) ;
        this.TranslateElements( document, 'LABEL', 'innerHTML',language) ;
        //this.TranslateElements( targetDocument, 'OPTION', 'innerHTML' ) ;
}
  </script>
 <script id=clientEventHandlersJS language=javascript>
<!--

function Button1_onclick() {
  TranslatePage( 'zh-cn' );
}

function Button2_onclick() {
 TranslatePage( 'en' );
}

//-->
</script>
</head>
 <body>
  <P>
   <label id="lLoginName">LoginName: </label><input id="Text1" type="text" name="Text1">
  </P>
  <P>
   <label id="lPassword">Password: </label><INPUT id="Text2" type="text" name="Text2">
  </P>
  <P><INPUT id="Button1" type="button" value="中文" onclick="return Button1_onclick()"><INPUT id="Button2" type="button" value="English" name="Button2" onclick="return Button2_onclick()"></P>
 </body>
</html>

posted on 2007-08-08 19:58  LanceLang  阅读(1672)  评论(1编辑  收藏  举报
使用Live Messenger联系我
关闭