幾年前,應用程序開發者可能僅僅提供對他們自己國家用戶(可能只有一種語言,有時兩種)的支持,表示日期,數值以及貨幣的格式是單一的。然而,基於Web技術的應用發展,在許多情況下,國界已經變得模糊不清。支持國際化(internationalization) 和本地化(localization)變得很重要。 Struts 構建在Java 平台之上,提供了構建國際化和本地化應用程序的幫助。主要內容有: · Locale – 支持國際化的基本的Java class是java.util.Locale。每一個 Locale 代表一個特定的國家和語言(加上一個可選的語言變量)的選擇,和一組假定的數值和日期的格式。 · ResourceBundle - java.util.ResourceBundle 類是支持多語消息的基本工具類。參見ResourceBundle 類的Javadocs 。 · PropertyResourceBundle – ResourceBundle的一個標準實現,它允許你使用"name=value"的語法定義資源。這對於文本資源來說非常方便。 · MessageFormat - java.text.MessageFormat 類允許你在運行時使用參數替代message string 中的一部分。The placeholder string {0} in the message is replaced by the first runtime argument, {1} is replaced by the second argument, and so on. · MessageResources – org.apache.struts.util.MessageResources 類使你像對待數據庫一樣處理一組資源,並允許你request a particular message string for a particular Locale (normally one associated with the current user) instead of for the default Locale the server itself is running in. 請注意the i18n support in a framework like Struts is limited to the presentation of internationalized text and images to the user. Support for Locale specific input methods (used with languages such as Japanese, Chinese, and Korean) is left up to the client device, which is usually a web browser. 對於一個國際化的應用程序,follow the steps described in the Internationalization document in the JDK documentation bundle for your platform to create a properties file containing the messages for each language. An example will illustrate this further: 假定你的源代碼創建在package com.mycompany.mypackage, 所以它被存儲在一個名叫com/mycompany/mypackage的目錄中 。創建一個叫作com.mycompany.mypackage.MyResources的資源包,你將在目錄com/mycompany/mypackage 下創建下列文件: · MyResources.properties – 包含了你的服務器的缺省語言資源。如果你的缺省語言是英語,可能會是這個樣子:prompt.hello=Hello · MyResources_xx.properties – 包含了同樣的資源,它們的ISO 語言代碼是"xx" (參見ResourceBundle Javadoc)。對於一個法語的版本,可能會是這樣:prompt.hello=Bonjour ,你可以創建你需要的任意多個資源包。當你在web application deployment descriptor中配置controller servlet時,你需要定義的一個初始化參數是應用程序使用的資源包名稱。在上面的例子中,將會是com.mycompany.mypackage.MyResources. action org.apache.struts.action.ActionServlet application com.mycompany.mypackage.MyResources <.../> 資源包應該放置在class path中。另一種方式是將MyResources.properties 文件保存在應用程序的classes 文件夾內。那麼,你可以簡單的在你的應用程序中使用"myResources"。 Just be careful it is not deleted if your build script deletes classes as part of a "clean" target. If it does, here is an Ant task to run when compiling your application that copies the contents of a src/conf directory to the classes directory: |