Home >Web Front-end >HTML Tutorial >Spring MVC and easyui internationalization_html/css_WEB-ITnose
Create the folder language under the webapp, add file in it, and name it language.properties, language_en.properties, language_zh_CN.properties. Among them, language.properties is the default resource file.
Add content in it in the following format:
language.properties
welcome=Welcome
language_en.properties
welcome=Welcome
Welcome = U6b22u8fce
Among them, Welcome is key, and this is used in JSP. Properties file Entering Chinese into unicode will be automatically converted into unicode.
Among them, & lt; proprity name = "basenename" value = "/language/language"/& gt; value is the resource file, no suffix name.properties.
3.1 Call internationalization in web
Add
<mvc:interceptors> <!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de --> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /></mvc:interceptors><!-- Saves a locale change using a session--><bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /><!-- 国际化文件 --><bean id="messageSource"class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/language/language" /> <property name="defaultEncoding" value="UTF-8"/></bean>
to
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
or
<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true"> Welcome</a>
changed to
<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true"> <spring:message code="welcome"/></a>
3.2 Call internationalization in javascript
<input type="text" value="welcome" />Add
Change
<input type="text" value='<spring:message code="welcome"/>' />
to
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
Note: 1.30e00583e8c86b7ef7d634797ffbf97d" as this will cause an error.
2. The key value in the _en file cannot use the or ' symbol.
$.messager.alert('info');3.3 Calling internationalization in java
Use
$.messager.alert('<spring:message code="message.info"/>');
in the controller where request is HttpServletRequest and model is Model.
4 Solving Garbled Codes
After completing the above steps, you will find that you enter Chinese in the XCenter interface, click OK, and write Chinese into the database , what is written is garbled code.
if(!model.containsAttribute("contentModel")){ RequestContext requestContext = new RequestContext(request); String welcome = requestContext.getMessage("welcome");}The solution is as follows:
In applicationContext.xml, change
Note: Do not add ?useUnicode=true&characterEncoding=UTF-8 after jdbc.url=jdbc:mysql://localhost:3306/demo in jdbc.properties. This is incorrect.
4.2 Some interfaces display Chinese garbled characters
<property name="url" value="${jdbc.url}"/>Then I found that most interfaces have added this sentence
bc723b4fd42fa4a789c881d7f28f33f3
<property name="url" value="${jdbc.url}?useUnicode=true&characterEncoding=UTF-8"/>can display Chinese normally, but part of the XCenter interface displays Chinese as???. After inspection, it was found that it is because in the controller The return value is String and marked as @ResponseBody. After checking the information, I found that the default encoding of @ResponseBody in Spring MVC is ISO-8859-1 (others are UTF-8. Does anyone know why another one is used here in the same framework? coding).
Solution:
🎜> becomes
Then you will find that the Chinese characters in some trees are still displayed as garbled characters. It is found that the return value is converted into JSON format.
@RequestMapping(value = "welcome", method = RequestMethod.POST)Solution:
Format the return value of tree to
@RequestMapping(value = "welcome", method = RequestMethod.POST, produces = "application/json; charset=utf-8")to
这个比较简单,使用如下代码就行
<a href="?locale=en" onclick="changeLanguage('en')">English</a><a href="?locale=zh" onclick="changeLanguage('zh_CN')">简体中文</a>
easyui中有些框架(如datagrid)有些内容是框架自带的,需要在93f0f5c25f18dab9d176bd4f6de5d30e中加入
<script type="text/javascript" src="js/locale/easyui-lang-zh_CN.js"></script>
那如何切换中英文呢
首先不要在93f0f5c25f18dab9d176bd4f6de5d30e中添加上面这句,然后在主界面93f0f5c25f18dab9d176bd4f6de5d30e中加入如下js语句
<script type="text/javascript"> var language=window.navigator.language; var userLanguage="${sessionScope['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE']}"; if(null != userLanguage&&userLanguage!=""){//not login language = userLanguage; } $(function(){ var src = 'js/locale' + '/easyui-lang-'+language.replace("-","_")+'.js';// when login in China the language=zh-CN $.getScript(src); });</script>
这是使用jquery加载js文件。
接下来将项目中需要替换的内容全部用步骤3中的方法替换就行了,这样国际化就完成了。