jQuery之前端国际化jQuery.i18n.properties

摘要:
jQuery.i18n.properties采用.properties文件对JavaScript进行国际化。jQuery.i18n.properties插件首先加载默认的资源文件,然后加载针对特定语言环境的资源文件,这就保证了在未提供某种语言的翻译时,默认值始终有效。资源文件命名有以下三种格式:basename.propertiesbasename_language.propertiesbasname_language_country.propertiesjQuery.i18n.propertiesAPIjQuery.i18n.properties的API只有几个:jQuery.i18n.properties()、jQuery.i18n.prop()、jQuery.i18n.browserLang(),当然也可以采用$.i18n.properties()、$.i18n.prop()、$.i18n.browserLang()的形式使用这些API。jQuery.i18n.properties该方法加载资源文件,其中settings是配置加载选项的一系列键值对。当key指定的值含有占位符时,可用使用jQuery.i18n.prop(key,val1,val2……jQuery.i18n.browserLang()用于获取浏览器的语言信息。

jQuery.i18n.properties是一款轻量级的jQuery国际化插件,能实现Web前端的国际化。

国际化英文单词为:Internationalization,又称i18n,“i”为单词的第一个字母,“18”为“i”和“n”之间单词的个数,而“n”代表这个单词的最后一个字母。jQuery.i18n.properties采用.properties文件对JavaScript进行国际化。jQuery.i18n.properties插件首先加载默认的资源文件(strings.properties),然后加载针对特定语言环境的资源文件(strings_zh.properties),这就保证了在未提供某种语言的翻译时,默认值始终有效。

资源文件命名有以下三种格式:

basename.properties

basename_language.properties

basname_language_country.properties

jQuery.i18n.properties API

jQuery.i18n.properties的API只有几个:jQuery.i18n.properties()、jQuery.i18n.prop()、jQuery.i18n.browserLang(),当然也可以采用$.i18n.properties()、$.i18n.prop()、$.i18n.browserLang()的形式使用这些API。

jQuery.i18n.properties(settings)

该方法加载资源文件,其中settings是配置加载选项的一系列键值对。各项配置项的具体描述如下:

选项描述类型可选
name资源文件的名称,例如strings或[strings1,strings2],前者代表一个资源文件,后者代表资源文件数组string或string[]
path资源文件所在路径string
mode

加载模式:

“vars”表示以JavaScript变量或函数的形式使用资源文件中的Key

“map”表示以Map的方式使用资源文件中的Key

“both”表示以同时使用两种方式。如果资源文件中的Key包含JavaScript关键字,则只能采用“map”。默认值是“vars”。

string
language

ISO-639指定的语言编码(例如“en”表示英文,“zh”表示中文),或者同时使用ISO-639和ISO-3166编码(例如:“en_US”,“zh_CN”)。如果不指定,则采用浏览器报告的语言编码。

string
cache

指定浏览器是否对资源文件进行缓存,默认值为false

boolean
encoding

加载资源文件时使用的编码。默认值为UTF-8

string
callback

代码执行完成时运行的回调函数

function

functionloadProperties() {
            jQuery.i18n.properties({//加载资浏览器语言对应的资源文件
                name : 'strings', //资源文件名称
                path : '/i18n/', //资源文件路径
                mode : 'map', //用Map的方式使用资源文件中的值
                language : 'zh',
                callback : function() {//加载成功后设置显示内容
                    $('.l-btn-text').each(function() {
                        $(this).text($.i18n.prop($(this).text()));
                    });
                }
            });
        }

jQuery.i18n.prop(key)

该方法以map方式使用资源文件中的值,其中key指的是资源文件中的key。当key指定的值含有占位符时,可用使用jQuery.i18n.prop(key,val1,val2……)的形式,其中val1,val2……对点位符进行顺序替换。

jQuery.i18n.browserLang()

用于获取浏览器的语言信息。

使用的方式

项目组织结构

jQuery之前端国际化jQuery.i18n.properties第1张

在i18n目录下,strings.properties对应默认翻译,strings_zh.properties对应中文翻译。

strings.properties

jQuery之前端国际化jQuery.i18n.properties第2张

strings_zh.properties

jQuery之前端国际化jQuery.i18n.properties第3张

<script type="text/javascript" src="http://t.zoukankan.com/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://t.zoukankan.com/js/jquery.i18n.properties-1.0.9.js"></script>
 <div id="content"> 
     <div> 
         <label id="label_username"></label> 
         <input type="text"id="username"></input> 
     </div> 
     <div> 
         <label id="label_password"></label> 
         <input type="password"id="password"></input> 
     </div> 
     <input type="button"id="button_login"/> 
 </div>
            
<script type="text/javascript">
    $(function(){
        jQuery.i18n.properties({
            name : 'strings', //资源文件名称
            path : '/i18n/', //资源文件路径
            mode : 'map', //用Map的方式使用资源文件中的值
            language : 'zh',
            callback : function() {//加载成功后设置显示内容
                $('#button-login').html($.i18n.prop('Login'));
                $('#label-username').html($.i18n.prop('User Name'));
                $('#label-password').html($.i18n.prop('Password'));
            }
        });
    });
</script>

下载地址:

https://code.google.com/p/jquery-i18n-properties/downloads/list

免责声明:文章转载自《jQuery之前端国际化jQuery.i18n.properties》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇TCP/IP网络编程系列之三(初级)物体检测丨Faster R-CNN详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android内核驱动开发中的Kconfig文件结构分析(图文)

1 Kconfig和Makefile毫不夸张地说,Kconfig和Makefile是我们浏览内核代码时最为依仗的两个文件。基本上,Linux 内核中每一个目录下边都会有一个Kconfig文件和一个Makefile文件。Kconfig和Makefile就好似一个城市的地图,地图引导我们去 认识一个城市,而Kconfig和Makefile则可以让我们了解一个内...

MD5Helper辅助类

DES加密和解密 public classMD5Helper { ///DES加密 ///sKey public string MD5Encrypt(string pToEncrypt, stringsKey) { DESCryp...

JAVA发送HTTP请求方式

1. HttpURLConnection 使用JDK原生提供的net,无需其他jar包; HttpURLConnection是URLConnection的子类,提供更多的方法,使用更方便。 package httpURLConnection; import java.io.BufferedReader; import java.io.InputStrea...

Spring学习笔记(14)——SpEL

是什么 Spring表达式语言全称为“Spring Expression Language”,缩写为“SpEL”,类似于Struts2x中使用的OGNL表达式语言,能在运行时构建复杂表达式、存取对象图属性、对象方法调用等等,并能与Spring功能完美整合。 表达式语言给静态Java语言增加了动态功能。 SpEL是单独模块,只依赖于core模块,...

devexpress实现单元格合并以及依据条件合并单元格

1、devexpress实现单元格合并非常的简单,只要设置属性【AllowCellMerge=True】就可以了,实现效果如下图: 2、但是在具体要求中并非需要所有的相同单元格都合并,可能需要其他的条件来控制合并。这个时候我们就需要在事件gridView1_CellMerge中来控制了。下图为根据最后一列判断是否合并单元格的效果图(其中第四列设置为不合并&...

Java 之 调用.Net的 WebService 整理

  最近做一个 java 调用 .net 服务的项目,其中 .net做了一个WebService,需要java来调用。   最开始.net的Service代码如下: using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocol...