jQuery之前端國際化jQuery.i18n.properties

來源:互聯網
上載者:User

標籤:odi   frame   bsp   檔案   地址   順序   each   encoding   toolbar   

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

 

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

用於擷取瀏覽器的語言資訊。

 

使用的方式

項目組織圖

在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 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.