國際化(Internationalization,縮寫為 i18n——取首尾字母 i 與 n,中間共 18 個字元)是指軟體在不修改程式碼的前提下,能根據使用者的語言與地區自動切換介面文字、日期格式、貨幣符號等資源。隨著全球化發展,支援多語言已成為 Web 應用的基本需求。
Struts 建立在 Java 平台之上,原生整合了 java.util.Locale、ResourceBundle 等核心類別,並提供 org.apache.struts.util.MessageResources 封裝,讓開發者能以統一 API 存取不同語系的資源檔。核心概念如下:
zh_CN、en_US),決定資源檔的選擇依據。.properties 文字檔。{0}、{1}…),解決語序差異問題。在 WEB-INF/classes 下建立 ApplicationResources.properties(預設語系,通常為英文):
label.username=username
label.password=password
button.submit=submit
button.reset=reset
先寫 UTF-8/Big5 明文檔 ApplicationResources_temp.properties:
label.username=用戶名
label.password=密碼
button.submit=確定
button.reset=重置
再用 JDK native2ascii 轉碼為 Unicode 轉義序列(Struts 1.x 需求):
native2ascii -encoding gbk ApplicationResources_temp.properties ApplicationResources_zh_CN.properties
產出內容範例:
label.username=\u7528\u6237\u540d
label.password=\u5bc6\u7801
button.submit=\u786e\u5b9a
button.reset=\u91cd\u7f6e
ApplicationResources_en.properties 內容同預設檔,明確指定語系碼。
在 struts-config.xml 中宣告:
<message-resources parameter="ApplicationResources" />
Struts 會自動依命名慣例載入 ApplicationResources_zh_CN.properties 等語系檔。
<html:form action="/login.do" method="post">
<bean:message key="label.username"/>
<html:text property="username" /><html:errors property="username"/>
<br/>
<bean:message key="label.password"/>
<html:password property="userpass" /><html:errors property="userpass"/>
<br/>
<input type="submit" value='<bean:message key="button.submit"/>' />
<input type="reset" value='<bean:message key="button.reset"/>' />
</html:form>
<bean:message> 會自動依使用者瀏覽器語系顯示對應文字。

瀏覽器語系設為中文 → 顯示中文;改為英文 → 自動切換英文,程式碼零修改。
native2ascii 轉碼;Struts 2 / 現代框架可直接支援 UTF-8 檔案。LookupDispatchAction)對中文鍵值支援有限,需改用英文鍵值搭配資源檔映射。掌握 Resource Bundle 機制,即可輕鬆打造全球化 Web 應用。