2013-01-10 23 views
1

<s:date name="mydate" format="dd/MM/yyyy HH:mm" />完美適用於我的日期符合法國標準。 但在我的jsp中很難寫。所以我必須構建if子句以根據語言環境進行切換。Struts2:internationalize s:date?

有沒有辦法將這種格式放到一個普通的屬性文件中?

我想:

format.date = {0,date,dd/MM/yyyy} 
format.time = {0,time,HH:mm} 

把我的全球的.properties但是當我嘗試只是一個<s:date name="mydate"><s:property value="mydate"/>它沒有考慮到。

回答

2

在屬性文件中創建本地日期格式,例如,與struts.date.format鍵:

struts.date.format = dd.MM.yyyy 

而且使用getText方法在<s:date>標籤format屬性獲取此日期格式:

<s:date name="date" format="%{getText('struts.datetime.format')}"/> 
+0

那麼明顯,那麼聰明。許多thx亞歷山大。 – Rydermark

+0

不客氣。 –

+0

此解決方案不適用於「d。MMM yyyy」等格式。月份名稱被設置爲偶爾使用的語言環境服務器。 – Bloodboiler

0

絕對。國際化(i18n)幾乎可以在所有框架中進行處理,因此最好花時間瞭解如何使其發揮作用,而不是編寫一些沒有干擾的錯誤定製解決方案。

隨着Struts2的,你必須確保有:在你的攔截器棧

  • I18nInterceptor;
  • 在struts.xml中定義的I18nInterceptor:<constant name="struts.custom.i18n.resources" value="global" />;
  • 您想要處理的每個語言環境的本地化,正確命名的global.properties文件;
  • request _locale`參數(如果您沒有更改瀏覽器中的語言)在調用您的動作時,讓I18nInterceptor能夠將請求劫持到正確的資源;
  • 適當的字符編碼在您的網頁上:<%@ page contentType=」text/html;charset=UTF-8″ %>

採取可看看導遊在網絡上太:

http://www.mkyong.com/struts2/struts-2-i18n-or-localization-example/

http://www.2bloggers.com/2011/12/localization-i18n-in-struts2.html

http://www.roseindia.net/struts/struts/struts2.2.1/tags/i18ntag.html

http://www.roseindia.net/struts/strutsinternationalization.shtml

http://www.roseindia.net/tutorials/I18N/internationalison-code.shtml