JQuery frontend internationalization jQuery. i18n. properties

Source: Internet
Author: User
Tags i18n

JQuery. i18n. properties is a lightweight jQuery internationalized plug-in that can internationalize the Web Front-end.

International English words: Internationalization, also known as i18n. "I" is the first letter of the word, and "18" is the number of words between "I" and "n, "n" represents the last letter of the word. JQuery. i18n. properties adopts the. properties file to internationalize JavaScript. JQuery. i18n. the properties plug-in first loads the default resource file (strings. properties), and then load the resource file (strings_zh.properties) for the specific language environment, which ensures that the default value is always valid when translation in a language is not provided.

Resource file naming can be in the following formats:

Basename. properties

Basename_language.properties

Basname_language_country.properties

JQuery. i18n. properties API

JQuery. i18n. there are only a few properties APIs: jQuery. i18n. properties (), jQuery. i18n. prop (), jQuery. i18n. browserLang (), of course, you can also use $. i18n. properties (), $. i18n. prop (), $. i18n. use these APIs in the form of browserLang.

JQuery. i18n. properties (settings)

This method loads the resource file. settings is a series of key-value pairs for configuring the loading options. The configuration items are described as follows:

Option Description Type Optional
Name Resource file name, such as strings or [strings1, strings2]. The former indicates a resource file, and the latter indicates a resource file array. String or string [] No
Path Path of the resource file String Yes
Mode

Loading Mode:

"Vars" indicates using the Key in the resource file in the form of JavaScript variables or functions

"Map" indicates using the Key in the resource file as Map

"Both" indicates that both methods are used. If the Key in the resource file contains the JavaScript keyword, you can only use "map ". The default value is "vars ".

String Yes
Language

The language encoding specified by the ISO-639 (for example, "en" represents English, "zh" represents Chinese), or uses both ISO-639 and ISO-3166 encoding (for example, "en_US", "zh_CN "). If this parameter is not specified, the language of the browser report is used.

String Yes
Cache

Specifies whether the browser caches resource files. The default value is false.

Boolean Yes
Encoding

The encoding used to load the resource file. UTF-8 by default

String Yes
Callback

The callback function that runs when the code is executed.

Function Yes

 

Function loadProperties () {jQuery. i18n. properties ({// load the resource file name: 'strings 'corresponding to the resource browser language, // The resource file name path:'/i18n/', // The resource file path mode: 'map', // use the resource file value language: 'zh ', callback: function () {// set the display content after loading successfully $ ('. l-btn-text '). each (function () {$ (this ). text ($. i18n. prop ($ (this ). text ()));});}});}

 

JQuery. i18n. prop (key)

This method uses the value in the resource file as map, where key refers to the key in the resource file. When the value specified by the key contains placeholders, jQuery. i18n. prop (key, val1, val2…) can be used ......) In the form of val1, val2 ...... Replace point operators in sequence.

JQuery. i18n. browserLang ()

Used to obtain the language information of the browser.

 

Usage

Project Organization Structure

In the i18n directory, strings. properties corresponds to the default translation, and strings_zh.properties corresponds to the Chinese translation.

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 ', // resource file name path:'/i18n/', // resource file path mode: 'map ', // use the language: 'zh ', callback: function () {// set the display content ('{button-login'{.html ($. i18n. prop ('login'); parameter ('your label-username'your .html ($. i18n. prop ('user name'); parameter ('{label-password'{.html ($. i18n. prop ('Password') ;}}) ;}); </script>

 

:

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.