An example of localized web development-jquery.i18n.properties

Source: Internet
Author: User

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

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.