1.建立資源文件
在webapp下建立文件夾language,在其中再添加file,命名分別為language.properties,language_en.properties,language_zh_CN.properties。其中language.properties為默認資源文件。
在其中添加內容,格式如下:
language.properties
welcome=Welcome
language_en.properties
welcome=Welcome
language_zh_CN.properties
welcome=\u6b22\u8fce
其中welcome為key,在jsp中調用使用的就是這個,漢語的需要使用unicode編碼(在eclipse的properties文件中輸入漢語會自動轉換成unicode。\u6b22\u8fce-歡迎)
2.配置springMVC
在demo-servlet.xml中添加如下
<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>
其中<property name="basename" value="/language/language" />value即為資源文件所在,不需要后綴名.properties。
3.調用國際化
3.1 在web中調用國際化
在jsp文件頭添加
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
在jsp HTML顯示內容處將內容更改,如
<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true"> Welcome </a>
更改為
<a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true"> <spring:message code="welcome"/> </a>
或
<input type="text" value="welcome" />
更改為
<input type="text" value='<spring:message code="welcome"/>' />
3.2 在javascript中調用國際化
在jsp文件頭中添加
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
將
$.messager.alert('info');
更改為
$.messager.alert('<spring:message code="message.info"/>');
注意:1.<spring>標簽不可以這樣寫"<spring:message code=\"message.info\"/>"這樣會出錯。
2.在_en文件中key的值不可以使用\或'符號。
3.3 在java中調用國際化
在controller中使用
if(!model.containsAttribute("contentModel")){ RequestContext requestContext = new RequestContext(request); String welcome = requestContext.getMessage("welcome"); }
其中request為HttpServletRequest,model為Model。
4 解決亂碼
4.1 中文輸入亂碼
完成了上面的步驟之后,會發現XCenter界面中輸入漢語,點擊確定,往數據庫中寫入漢語時,寫入的是亂碼。
解決辦法如下:
在applicationContext.xml中的
<property name="url" value="${jdbc.url}"/>
更改為
<property name="url" value="${jdbc.url}?useUnicode=true&characterEncoding=UTF-8"/>
注意:不要在jdbc.properties中的jdbc.url=jdbc:mysql://localhost:3306/demo之后添加?useUnicode=true&characterEncoding=UTF-8,這樣是不正確的。
4.2 部分界面顯示中文亂碼
接下來發現在大部分界面因為添加了這句
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
可以正常顯示中文了,但是XCenter部分界面中文顯示為???,經過檢查發現是因為在controller中返回值為String,而且標記為@ResponseBody,查資料發現是因為Spring MVC中@ResponseBody默認編碼是ISO-8859-1(其他的都是UTF-8,誰知道為什么同一個框架中這里要用另外一種編碼)。
解決辦法:
在
@RequestMapping(value = "welcome", method = RequestMethod.POST)
中添加produces = "application/json; charset=utf-8"
變成
@RequestMapping(value = "welcome", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
然后會發現在部分的tree中中文仍然顯示為亂碼,發現是因為返回值轉換成JSON格式的原因。
解決辦法:
tree的返回值格式化將
JSON.toJSONStringWithDateFormat(rs.getDefaultModel(), "yyyy-MM-dd HH:mm:ss");
更改為
JSON.toJSONString(rs.getDefaultModel());
5 中英文切換
5.1 資源文件的中英文切換
這個比較簡單,使用如下代碼就行
<a href="?locale=en" onclick="changeLanguage('en')">English</a> <a href="?locale=zh" onclick="changeLanguage('zh_CN')">簡體中文</a>
5.2 easyui框架的中英文切換
easyui中有些框架(如datagrid)有些內容是框架自帶的,需要在<head>中加入
<script type="text/javascript" src="js/locale/easyui-lang-zh_CN.js"></script>
那如何切換中英文呢
首先不要在<head>中添加上面這句,然后在主界面<head>中加入如下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中的方法替換就行了,這樣國際化就完成了。