JQuery.i18n.properties is a lightweight jQuery internationalization plugin that enables the internationalization of the Web front end.
The internationalized English word is: internationalization, also called i18n, "I" is the first letter of the word, "18" is the number of words between "I" and "N", and "N" represents the last letter of the word. JQuery.i18n.properties uses the. properties file to internationalize JavaScript. The JQuery.i18n.properties plugin loads the default resource file (strings.properties) First, and then loads the resource file for the specific locale (strings_ Zh.properties), which guarantees that the default value is always valid when translations for a language are not available.
resource files are named in the following three formats:
basename.properties
basename_language.properties
basname_language_country.properties
jQuery.i18n.properties API
JQuery.i18n.properties has only a few APIs: JQuery.i18n.properties (), JQuery.i18n.prop (), JQuery.i18n.browserLang (), Of course, these APIs can also be used in the form of. I18n.properties (),. I18n.prop (), $.i18n.browserlang ().
jQuery.i18n.properties (Settings)
This method loads the resource file, where settings is a series of key-value pairs that configure the load option. The specific descriptions of each configuration item are as follows:
Options |
Describe |
Type |
Options available |
Name |
The name of the resource file, such as strings or [STRINGS1,STRINGS2], which represents a resource file, which represents an array of resource files |
String or string[] |
Whether |
Path |
The path where the resource file resides |
String |
Is |
Mode |
Load mode: "VARs" means using a key from a resource file in the form of a JavaScript variable or function "Map" means using the key from the resource file in map mode "Both" means to use both ways. If the key in the resource file contains a JavaScript keyword, you can only use "map." The default value is "VARs". |
String |
Is |
Language |
ISO-639 The specified language encoding (e.g. "en" for English, "zh" for Chinese), or both ISO-639 and ISO-3166 encoding (e.g. "en_US", "ZH_CN"). If not specified, the language encoding that is reported by the browser is used. |
String |
Is |
Cache |
Specifies whether the browser caches the resource files, the default value is False |
Boolean |
Is |
Encoding |
The encoding to use when loading the resource file. The default value is UTF-8 |
String |
Is |
Callback |
callback function to run when code execution is complete |
function |
Is |
functionloadProperties () {jQuery.i18n.properties ({//resource file for the loaded browser languageName: ' Strings ',//Resource File namePath: '/i18n/',//Resource file pathMode: ' Map ',//use the values in the resource file in map modeLanguage: ' zh ', Callback:function() {//setting the display after a successful load$ ('. L-btn-text '). each (function() { $( This). Text ($.i18n.prop ($ ( This). Text ())); }); } }); }
JQuery.i18n.prop (Key)
The method uses the value in the resource file in map mode, where key refers to the key in the resource file. When the value specified by key contains a placeholder, use JQuery.i18n.prop (Key,val1,val2 ...) is available. The form of which val1,val2 ... The order is replaced by the dot character.
JQuery.i18n.browserLang ()
Used to obtain language information for the browser.
How to use
Project Organizational Structure
In the i18n directory, strings.properties corresponds to the default translation, 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>
<DivID= "Content"> <Div> <labelID= "Label_username"></label> <inputtype= "text"ID= "username"></input> </Div> <Div> <labelID= "Label_password"></label> <inputtype= "Password"ID= "Password"></input> </Div> <inputtype= "button"ID= "Button_login"/> </Div>
<script type= "Text/javascript" > $(function() {jQuery.i18n.properties ({name:' Strings ',//Resource File namePath: '/i18n/',//Resource file pathMode: ' Map ',//use the values in the resource file in map modeLanguage: ' zh ', Callback:function() {//setting the display after a successful load$ (' #button-login '). HTML ($.i18n.prop (' login '))); $(' #label-username '). HTML ($.i18n.prop (' User Name '))); $(' #label-password '). HTML ($.i18n.prop (' password '))); } }); }); </script>
jquery Front-end internationalization jQuery.i18n.properties