struts 和I18n:
就是 International 後面18個字元。。具體意義省略。
其實現原理是:
服務端維護多套對應的資源檔,對應於不同的語言(這些資源檔獨立於代碼存在)。
訪問發送到伺服器時,伺服器自動識別用戶端所使用的語言環境。
伺服器根據客戶的語言環境選擇合適的資源,渲染入視圖,然後返回給用戶端。
struts 實現I18n
struts的struts-config 裡可以定義一個
<message-resources> element, 該標誌能夠導致struts引用到不同的資源檔。
實現的方式很簡單:
MessageResources_zh_CN
message.hello=hello..(cn)
sturts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
</form-beans>
<global-exceptions>
</global-exceptions>
<action-mappings>
<action path="/view"
type="com.bss.test.i18n.ActionI18N">
<forward name="success" path="/ui/view.jsp" redirect="false" />
</action>
</action-mappings>
<message-resources key="lala" parameter="com.bss.test.i18n.MessageResources" />
</struts-config>
view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<b>This page is for test I18n.<b><br>
Message:
<bean:message key="message.hello" bundle="lala"/>
</body>
</html>
這裡,這個<message-resources key="lala" parameter="com.bss.test.i18n.MessageResources" />
和<bean:message key="message.hello" bundle="lala"/>中的lala想對應,也就是說,可以有多個不同的MessageSources.
只要他們<message-resources key="lala" parameter="com.bss.test.i18n.MessageResources" />中的key不一樣就可以了。
可以自由的安排設定檔的部署。