Let the storm money come!

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));

 

posted @   精密~顽石  阅读(9455)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
在通往地狱的路上,加班能使你更快到达。
点击右上角即可分享
微信分享提示