jquery.i18n.properties.js国际化的一个简单应用

本文介绍了一个使用jQuery i18n.properties.js进行国际化的简单应用示例。通过创建HTML页面,引入jQuery库和jQuery.properties.js,然后设置资源文件(如en_properties和zh_CN.properties),并编写JavaScript代码来根据用户操作或浏览器语言切换显示内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于国际化一个简单应用,写在这里记住,直接贴代码。

新建HTML页面,引入jquery 和 jquery.properties.js。

    <!--引入资源-->
    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="i18n/jquery.i18n.properties.js"></script>

要准备切换的资源:

    <div class="">
        <b class="i18n"  id="usernameText">用户名</b>:<input type="text" class="i18n" id="username" placeholder="请输入用户名" ><br/>
        <b class="i18n"  id="passwordText">密码</b>:<input type="password" class="i18n" id="password" /><br/>

        <button onclick="loadProperties()">切换</button>
        <button onclick="alertFun()">弹出框提示</button>
    </div>

这里通过点击按钮"切换"来实现中英文的切换。实际上在开发过程中应该读取浏览器的当前语言来进行切换。

再接着建立资源文件,先建立文件夹i18n,接着继续建立文件夹locate,下面建立两个文件:en_properties和zh_CN.properties。

en_properties:

index_username_placeholder = please input username
index_usernameTextText = username
index_passwordTextText = password
MessageSuccess = operatie success

zh_CN.properties:

index_username_placeholder = 请输入用户名
index_usernameTextText = 用户名
index_passwordTextText = 密码
MessageSuccess = 操作成功

目录:

最后用js代码来实现上面的逻辑:

//首先进行初始化操作  应该和浏览器语言保持一致,这里默认中文
    $(function(){
        loadProperties();
    })

    var language = 'zh_CN';

    function loadProperties(){
        $.i18n.properties({
            name : language,//资源文件名称
            path:'i18n/locale/',//资源所在的路径
            mode:'map',//用Map的方式使用资源文件中的值
            callback:function(){//加载成功后设置显示内容
                if(language == "en"){
                    language = 'zh_CN';
                }
                else{
                    language = 'en';
                }
                //手动一个一个展示
                // $("#username").attr("placeholder",$.i18n.prop('index_username_placeholder'));
                // $("#usernameText").text($.i18n.prop('index_usernameTextText'));
                // $("#passwordText").text($.i18n.prop('index_passwordTextText'));

                //以遍历的方式进行 创建相应的规则  key和ID结合 谨慎使用
                //控制文本
                $(".i18n").each(function (index,e) {
                    $(this).text($.i18n.prop('index_' + this.id + "Text"));
                });
                //控制placeholder
                $(".i18n").each(function (index,e) {
                    //判断是否存在该属性  如果存在就赋值
                    if((typeof($(this).attr("placeholder")) != "undefined")){
                        $(this).attr("placeholder",$.i18n.prop('index_' + $(this).attr("id") + "_placeholder"));
                    }

                });

            }
        })
    }

    function alertFun(){
        alert($.i18n.prop("MessageSuccess"));
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值