zoukankan      html  css  js  c++  java
  • JS 实现中英文翻译

         缺点就是还是会闪出中文,但是效果还行。

        

        var langPackage = {
    "主题":"Title",
        "下一页":"NextPage",
        "末页":"LastPage",
        "首页":" FirstPage ",
        "上一页":" PreviousPage ",
        "待办工作":"MyTasks",
        "中":"Middle",
        "每页":" EachPage ",
        "条":" Record ",
        "共":" Total ",
        "页":" Page ",
        "第":" Current ",
        "工作主题":" ProcTitle"
        };

    执行遍历 DOM 的逻辑

        /* 
            主调函数
            在 Jquery的 .read方法里调用 ReplaceChildChs($(document));
            或者页面的最后调用
            ReplaceChildChs($(document));
        */
        function ReplaceChildChs(nodeObj){
            // if($("#hdfUseLang").val()=="CN")return;
            if (nodeObj.children().length > 0){
                nodeObj.children().each(function(){
                   ReplaceChildChs($(this));
        //            if ($(this)[0].nodeName.toUpperCase() == "TD"){
                    FindChsAndReplaceIt($(this));
        //            }
                });
            } else {
                FindChsAndReplaceIt(nodeObj);
            }
        }
    
        // 直接替换html 的一种设想,但总是报错
        function JustReplaceChsDom(nodeObj){
            var pat = new RegExp("[u4e00-u9fa5]+","g"); // 匹配中文的正则表达式
            var str = $(nodeObj).html();
            while((arr = pat.exec(str)) != null){
                if (langPackage[arr[0]]){
                    str = str.replace(arr[0], langPackage[arr[0]]);
                }
            }
            $(nodeObj).html(str);
        }
    
        function FindChsAndReplaceIt(nodeObj){
            var pat = new RegExp("[u4e00-u9fa5]+","g");
            if ((nodeObj.text() || nodeObj.val() || nodeObj.attr("title")) 
                && (pat.exec(nodeObj.text()) || pat.exec(nodeObj.val()) || pat.exec(nodeObj.attr("title")) )){
                var str = ""
                if (nodeObj.text()){
                    str = nodeObj.text();
                    ReplaceValue(str, nodeObj, "text");
                }
                if (nodeObj.val()){
                    str = nodeObj.val();
                    ReplaceValue(str, nodeObj, "val");
                }
                if (nodeObj.attr("title")){
                    str = nodeObj.attr("title");
                    ReplaceValue(str, nodeObj, "title");
                }
            }
        } 
    
        function ReplaceValue(str, nodeObj, attrType){
          var arr;
          var pat = new RegExp("[u4e00-u9fa5]+","g");
          while((arr = pat.exec(str)) != null){
            if (langPackage[arr[0]]){
                str = str.replace(arr[0], langPackage[arr[0]]);
                
                if (attrType == "text"){
                    nodeObj.text(str);
                }
                else if (attrType == "val"){
                    nodeObj.val(str);
                }
                else if (attrType == "title"){
                    nodeObj.attr("title", str);
                }
            }
          }
        }

    ReplaceChildChs($(document));

  • 相关阅读:
    杭电 Problem
    杭电Problem 5053 the sum of cube 【数学公式】
    杭电 Problem 2089 不要62 【打表】
    杭电 Problem 4548 美素数【打表】
    杭电 Problem 2008 分拆素数和 【打表】
    杭电 Problem 1722 Cake 【gcd】
    杭电 Problem 2187 悼念512汶川大地震遇难同胞——老人是真饿了【贪心】
    杭电Problem 1872 稳定排序
    杭电 Problem 1753 大明A+B
    东北林业大 564 汉诺塔
  • 原文地址:https://www.cnblogs.com/zlfucku/p/3425626.html
Copyright © 2011-2022 走看看