Using Jquery.i18n.properties.js to realize JS internationalization

Source: Internet
Author: User

The first two days received a task, the previous system has been done to achieve internationalization, the previous system with the spring framework, internationalization or relatively simple. But some hints are written in the JS file inside, search some, found a JS international framework jquery.i18n.properties.js, with some or more convenient.

First introduced JS file, this framework is dependent on jquery, so jquery should be introduced first, because the project with Requirejs, so in the main file to introduce a bit

require.config ({    baseurl:  contextpath +  "/js",    paths: {         jquery:  ' util/jquery-1.9.1 ',        jqueryi18n:  ' util/ Jquery.i18n.properties '     },    shim: {          ' jquery ': {             exports:  ' $ '         },          ' jqueryi18n ':  {            deps:  [  "jquery"  ],            exports:   ' jqueryi18n '         }    }}); 

Next, create a file folder in the resource file directory to put the properties file

Because the system can switch languages manually, the language settings are written in a cookie,

var Setlanguagecookie = function (language) {var d = new Date ();    D.settime (D.gettime () + (30 * 24 * 60 * 60 * 1000));    var expires = "expires=" + d.toutcstring (); Document.cookie = "language=" + escape (language) + ";" + Expires + "; Path=/";};

Call Jquery.i18n.properties.js's $.i18n.properties () method. If there is a language setting in the cookie, it is used in the cookie, if not in the language of the browser.

var lan = Navigator.language | | Navigator.userlanguage;var arrstr = Document.cookie.split (";");    for (var i = 0; i < arrstr.length; i++) {var temp = arrstr[i].split ("=");    if (temp[0] = = ' language ') {lan = unescape (temp[1]); }}$.i18n.properties ({name: ' message ', Path:contextpath + '/i18n/', Mode: ' Map ', Language:lan});

Use spring to remember to add the I18N resource directory to the configuration file

<mvc:resources mapping= "/i18n/**" location= "/resources/i18n/"/>

Use Cookielocaleresolver to read the value of the cookie and configure the CookieName

<bean id= "Localeresolver" class= "Org.springframework.web.servlet.i18n.CookieLocaleResolver" > <property Name= "CookieName" value= "language"/></bean>

Write key:site.success in Message_en.properties and message_zh.properties

site.success=success!site.success= Success!

The above configuration is ready to use, call $.i18n.prop (key) to display hints in different languages

Alert ($.i18n.prop (' site.success '));

English environment:

In the Chinese environment:


Using Jquery.i18n.properties.js to realize JS internationalization

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.