posts - 5, comments - 24, trackbacks - 0, articles - 20
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          資源文件中復(fù)合消息的實現(xiàn)

          Posted on 2006-09-25 00:48 kook 閱讀(211) 評論(0)  編輯  收藏 所屬分類: Struts
          在Struts框架中,往往需要對資源文件進行配置,從而實現(xiàn)國際化的目的。

          資源文件中允許使用靜態(tài)和動態(tài)文本。通過動態(tài)文本來動態(tài)的設(shè)定其顯示的內(nèi)容,從而能更好的減少代碼的冗余,然而動態(tài)文本中的參數(shù)指定,是通過什么來實現(xiàn)的呢?

          下面是ApplicationResources.properties資源文件中的內(nèi)容:

          1?#?Resources?for?parameter?'com.kook.struts.ApplicationResources'
          2?#?Project?P/ Demo_242103
          3?

          4?
          5?
          6?hello={0}?is?the?brother?of?{1 }
          7?hello.brother=
          zhangsan
          8?hello.person=lisi

          通常我們可以在驗證框架的配置文件中來配置其動態(tài)參數(shù)的值,如:

          1?<field?property="name"?depends="required">
          2?????????????????<arg0?name="required"?key="hello.brother"/>
          3??</field>

          但是具體的實現(xiàn)可以通過java.text.MessageFormat類來實現(xiàn)

          1???????MessageResources?messages?= ?getResources(request);
          2?????????String?title?=?messages.getMessage("hello"
          );
          3?????????String[]?arr?=?new?String[2
          ];
          4?????????arr[0]?=?messages.getMessage("hello.brother"
          );
          5?????????arr[1]?=?messages.getMessage("hello.person"
          );
          6?????????String?newMessage?=
          ?MessageFormat.format(title,?arr);
          7?????????System.out.println(newMessage);

          最后輸出:zhangsan is the brother of lisi

          ?

          ?

          主站蜘蛛池模板: 台东市| 辽阳市| 平阴县| 桓台县| 永德县| 得荣县| 澄江县| 浏阳市| 广宁县| 衡南县| 钟山县| 凤城市| 车致| 从化市| 辽宁省| 宾阳县| 漠河县| 张家口市| 哈密市| 海盐县| 黔江区| 右玉县| 镇巴县| 巨鹿县| 鹤峰县| 读书| 遂平县| 冷水江市| 千阳县| 麟游县| 永胜县| 石棉县| 仁寿县| 太康县| 贵州省| 万年县| 长岛县| 都兰县| 荔波县| 新余市| 景谷|