Keywords: web localization, jquery,jquery.i18n.properties.
Operating environment: Chrome, IE.
This article describes using Jquery.i18n.properties to localize the front-end of a Web site and support multiple languages. Site content is displayed according to the language of the browser settings.
1. The front-end folder structure is as follows:
2.index.html file
<!DOCTYPE HTML><HTML><Head> <titledata-localize= "Common.title"></title> <Scriptsrc= "/javascripts/3p/jquery-1.8.2.min.js"></Script> <Scriptsrc= "/javascripts/3p/jquery.i18n.properties-min-1.0.9.js"></Script> <Scriptsrc= "/javascripts/main.js"></Script></Head><Body> <Divclass= "Home-area"ID= "Home"data-localize= "Common.text"></Div></Body></HTML>
Need to localize Common.title and Common.text.
3.properties
Main.properties is used by default if no matching language is found.
Common.title = Loc Sample-= welcome!
Main_en.properties, if the browser language is en_*, the file will be used.
Common.title = Loc Sample-= welcome!
Main_zh.properties, if the browser language is zh_*, the file will be used.
Common.title = Loc Sample-Home
Common.text = welcome!
4.main.js
$ (document). Ready (function() {loadProperties (' Main ', '/strings/main/');});functionloadProperties (name, path, Lang) {varLang = lang | |Navigator.language; JQuery.i18n.properties ({name:name, Path:path, mode:' Map ', Language:lang, callback:function() { $("[Data-localize]"). each (function() { varElem = $ ( This), Localizedvalue= Jquery.i18n.map[elem.data ("Localize")]; if(Elem.is ("Input[type=text]") | | elem.is ("input[type=password]") | | elem.is ("input[type=email]") {elem.attr ("Placeholder", Localizedvalue); } Else if(Elem.is ("Input[type=button]") | | elem.is ("input[type=submit]") {elem.attr ("Value", Localizedvalue); } Else{elem.text (localizedvalue); } }); } });}
The Loadproperties function is called after the page has finished loading. Loadproperties finds the matching properties file according to the browser language, and then replaces the page string contents.
5. Set up a Web server to run index.html.
Open index.html directly, there will be cross-domain access issues, resulting in the inability to access the properties file.
So you need to build a Web server. How to set up a Web server please refer to: http://www.cnblogs.com/ldlchina/p/4054974.html
6. Operation Result:
English:
Chinese:
An example of localized web development-jquery.i18n.properties