jQuery.i18n.properties是一款輕量級的jQuery國際化插件,能實現Web前端的國際化
資源文件命名有以下三種格式:
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 | 是 |
function loadProperties() { 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()
用於獲取瀏覽器的語言信息。
使用的方式1
項目組織結構
在i18n目錄下,strings.properties對應默認翻譯,strings_zh.properties對應中文翻譯。
strings.properties:
strings_zh.properties:
<script type="text/javascript" src="/js/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="/js/jquery.i18n.properties-1.0.9.js"></script>
<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>
$(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>
使用的方式2
jquery+angularJs
<script type="text/javascript" src="../plug-in/jquery/jquery2.1.0.js"></script>
<script type="text/javascript" src="../plug-in/angular/angular.js"></script>
<script type="text/javascript"
src="../plug-in/jquery/jquery.i18n.properties-1.0.9.js"></script>
定義propties 文件
1、messageResources_en_US.properties內容如下
language=language
firstName=firstName
lastName=lastName
age=age
sex=sex
birthday=birthday
email=email
address=address
2、messageResources_zh_CN.properties
language=語言
firstName=姓
lastName=名字
age=年齡
sex=性別
birthday=生日
email=電子郵箱
address=地址
3、JS代碼
<script>
var app = angular.module("testint", []);
function test($scope) {
$scope.info = null;
jQuery.i18n.properties({// 加載資瀏覽器語言對應的資源文件
name : 'messageResources', // 資源文件名稱
language : 'en_US', //默認爲英文當改爲zh_CN時頁面顯示中文語言
path : '../resource/', // 資源文件路徑
mode : 'map', // 用 Map 的方式使用資源文件中的值
callback : function() {// 加載成功後設置顯示內容
$scope.info = $.i18n.map;
}
});
}
</script>
</head>
<body>
<div ng-controller="test">
{{info.address}} <br>{{info.firstName}}<br>
{{info.language}}
</div>
</body>
下載地址:
https://code.google.com/p/jquery-i18n-properties/downloads/list