jquery.i18n的国际化

html中写好样式

    <div class="container">
        <div class="top">
            <!-- 切换按钮 -->
            <div class="top1">
                <div id="chinese" class=" AA">切换为中文</div>
                <div id="english" class=" AA">switch to english</div>
                <div id="Vietnam" class=" AA">Việt nam</div>
            </div>
        </div>
        <!-- 显示的主体内容  -->
        <div class="register-box">
            <div class="telphone">
                <input type="text" id="telphone" i18n="footer.phone" placeholder="footer.phone" value="">
            </div>
            <div class="password">
                <input type="text" id="password" i18n="footer.password" placeholder="footer.password" value="">
                <span id="code" style="display:none"></span>
            </div>
            <div class="telcode">
                <input type="text" id="gg" i18n="footer.yanzhen" placeholder="footer.yanzhen" value="">
                <button id="getcode" i18n="footer.Getverification">Get verification code</button>
            </div>
            <div class="registerbtn" id="registerbtn">
            </div>
            <div class="bottomtitle">
                <span i18n="footer.Apply">By clicking 'apply now' you agree</span><span id="register"
                    i18n="footer.registration">《User registration agreement》</span>
            </div>


        </div>
    </div>

引入css文件 如<link rel="stylesheet" href="./style/register.css"> 写好css样式

input {
    border: 0;
    background: #fbe8ea;
}

button {
    border: 0;
}

.container {
    width: 7.5rem;
    height: 100%;
    background: #e96459;
}

.logo,
.background {
    width: 100%;
}

.background img,
.logo img {
    width: 100%;
}

.register-box {
    margin: auto;
    width: 6.9rem;
    height: 5.2rem;
    background-size: 100% 100%;
    padding-top: 0.5rem;
    background: #fbe8ea
}

.telphone,
.password,
.telcode {
    width: 5.9rem;
    margin-left: .7rem;
    height: .7rem;
    line-height: .7rem;
    border-radius: .14rem;
    border: 1px solid #bfbfbf;
}

#telphone,
#password,
#telcode {
    width: 5rem;
    border: none;
    outline: none;
    margin-left: .2rem;
    font-size: .28rem;
}
#gg {
    width: 3rem;
    border: none;
    outline: none;
    margin-left: .2rem;
    font-size: .28rem;
}
.password {
    margin-top: .4rem;
    margin-bottom: .4rem;
}

#code {
    float: right;
    margin-right: 0.5rem;
}

#codetxt {
    float: right;
    margin-right: .5rem
}

.registerbtn {
    margin-left: .7rem;
    width: 5.9rem;
    height: .9rem;
    line-height: .9rem;
    border-radius: .9rem;
    margin-top: .35rem;
    border: none;
    background-image:url(../image/enbutton.png);
    background-repeat:  no-repeat ;
    align-items:center;
    background-size: 100% 100%;
    overflow: hidden;
}

.display{
    display: none;
}

#getcode {
    margin-top: 0.1rem;
    width: 2.1rem;
    height: 80%;
    text-align: center;
    font-size: .24rem;
    color: #e45849;
    background: #fbe8ea;
    font-weight: 600;
    float: right;
    border-left: 1px solid #bfbfbf;
}

.bottomtitle {
    width: 6.5rem;
    margin-left: .5rem;
    border: none;
    height: .4rem;
    margin: 0rem auto;
    margin-top: 0.1rem;
    font-size: .16rem;
    color: rgb(177, 177, 177);
    transform: scale(0.8);
}

#register {
    color: #da362d;
}
.top{
    display: inline-block;
   background-color:  #FBE9EB;
   height: 30px;
   width: 100%;
   margin: 0 auto;
}
.top1{
   
    
    text-align: center;
    line-height: 30px;
    
    
    
    
}
.AA{
    display: inline-block;
    border-right: 1px solid #D2402A;
    text-align: center;
    line-height: 20px;
    padding-right: 5px;
    height: 20px;
    color: #CF3216;
    
    font-weight: 700;
}

引入jquery.i18n.min.js  如

<script type="text/javascript" src="./i18n/jquery.i18n.min.js"></script>

./i18n/jquery.i18n.min.js通用代码

(function ($) { $.fn.extend({ i18n: function (options) { var defaults = { lang: "", defaultLang: "", filePath: "i18n/", filePrefix: "i18n_", fileSuffix: "", forever: true, callback: function () { } }; function getCookie(name) { var arr = document.cookie.split('; '); for (var i = 0; i < arr.length; i++) { var arr1 = arr[i].split('='); if (arr1[0] == name) { return arr1[1] } } return '' }; function setCookie(name, value, myDay) { var oDate = new Date(); oDate.setDate(oDate.getDate() + myDay); document.cookie = name + '=' + value + '; expires=' + oDate }; var options = $.extend(defaults, options); if (getCookie('i18n_lang') != "" && getCookie('i18n_lang') != "undefined" && getCookie('i18n_lang') != null) { defaults.defaultLang = getCookie('i18n_lang') } else if (options.lang == "" && defaults.defaultLang == "") { throw "defaultLang must not be null !" }; if (options.lang != null && options.lang != "") { if (options.forever) { setCookie('i18n_lang', options.lang) } else { $.removeCookie("i18n_lang") } } else { options.lang = defaults.defaultLang }; var i = this; $.getJSON(options.filePath + options.filePrefix + options.lang + options.fileSuffix + ".json", function (data) { var i18nLang = {}; if (data != null) { i18nLang = data } $(i).each(function (i) { var i18nOnly = $(this).attr("i18n-only"); if ($(this).val() != null && $(this).val() != "") { if (i18nOnly == null || i18nOnly == undefined || i18nOnly == "" || i18nOnly == "value") { $(this).val(i18nLang[$(this).attr("i18n")]) } } if ($(this).html() != null && $(this).html() != "") { if (i18nOnly == null || i18nOnly == undefined || i18nOnly == "" || i18nOnly == "html") { $(this).html(i18nLang[$(this).attr("i18n")]) } } if ($(this).attr('placeholder') != null && $(this).attr('placeholder') != "") { if (i18nOnly == null || i18nOnly == undefined || i18nOnly == "" || i18nOnly == "placeholder") { $(this).attr('placeholder', i18nLang[$(this).attr("i18n")]) } } }); options.callback() }) } }) })(jQuery);

 

js代码,ready读取

        $(document).ready(function () {
            /*默认语言*/
            var defaultLang = "en";

            $("[i18n]").i18n({
                defaultLang: defaultLang,
                filePath: "i18n/",
                filePrefix: "i18n_",
                fileSuffix: "",
                forever: true,
                callback: function () {
                    console.log("i18n has been completed.");



                }
            });
            /*切换为中文 - 按钮*/
            $("#chinese").click(function () {
                $("[i18n]").i18n({
                   
                    defaultLang: "cn"

                });

            });
            /*切换为英文 - 按钮*/
            $("#english").click(function () {
                $("[i18n]").i18n({
                    
                    defaultLang: "en",
                });

            });
            /*切换为越南 - 按钮*/
            $("#Vietnam").click(function () {
                $("[i18n]").i18n({
                  
                    defaultLang: "vi",
                });
               

                

            });

        });

新建文件 cn.json en.json 

如cn.json

{
    "footer.about" :"关于",
    "footer.contact": "联系我们",
    "footer.phone": "请输入手机号",
    "footer.yanzhen": "请输入校验码",
    "footer.password": "请输入密码",
    "footer.Getverification": "获取验证码",
    "footer.Apply": "点击“立即申请”即表示您同意",
    "footer.registration": "《用户注册协议》"
}

 

最后的效果图  

posted @ 2019-08-19 16:49  guanqinghua  阅读(200)  评论(0编辑  收藏  举报