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