處理國際化問題得在先配置好struts的環境下才能進行的,關于如何配置struts環境,網上搜一下,已經一大巴了,沒必要我再在這里羅嗦了。
主要說明一下如何解決struts下惡國際化問題,剛摸索這個問題的時候走了很多錯路,害我整了一兩個小時,寫下來主要是為了以后自己或者初學的人不要再走錯路。
真正要實現struts國際化很簡單。
struts國際化訪問的都是目錄下的properties資源文件。我們先建幾個資源文件
ApplicationResource_zh.bak(中文源文件)
index.jsp.welcome=你好
index.jsp.userid=用戶編號
index.jsp.pass=用戶密碼
建Application_en.properties
index.jsp.welcome=welcome
index.jsp.userid=User ID
index.jsp.pass=Password
現在將ApplicationResource_zh.bak轉換為unicode編碼的文件
native2ascii -encoding -GBK ApplicationResource_zh.bak ApplicationResource_zh_CN.properties
native2ascii -encoding -Big5 ApplicationResource_zh.bak ApplicationResource_zh_TW.properties
注意:資源文件的命名規則:ApplicationResource_國家_語言。還有千萬注意大小寫。我自以為是的吧zh弄成大寫,害我整了好久都沒效果,真是丟臉
接著可以在struts-config.xml中配置資源文件
?<message-resources parameter="net.skycity.ApplicationResource" />
jsp中頭部設置為:
<%@ page contentType="text/html; charset=UTF-8"%>
默認國際化
<%@ taglib uri="/struts-bean" prefix="bean"%>
<%@ taglib uri="/struts-html" prefix="html"%>
<%@ taglib uri="/struts-logic" prefix="logic"%>
<%@ page import="org.apache.struts.Globals"%>
<html:html locale="true">
?<bean:message key="index.jsp.welcome"/>
</html:html>
變更不同的國家,可以在瀏覽器的工具—>Internet選項->語言設置
刷新頁面,就會顯示不同國家的資源文件名
Lyyb2001