『Spring Recipes』第二章メモ:Resoliving Text Messages
2395 ワード
『Spring Recipes』第二章メモ:Resoliving Text Messages
問題
国際化の際には、複数の異なるlocaleのリソースファイルを使用する必要があります.
ソリューション
ApplicationインターフェースはKeyによって異なるLocaleからのレスポンスを提供しています. bundleでテキスト情報を取得する機能です.
(1)まずレスポンスを保証する bundleは正しいフォーマットです.basename[u langage].propertiesは、classipathのルートディレクトリの下に保存されます.
(2)設定ファイルにレスポンスを宣言する bundleのbasename:org.springframe.com.support.Resourceのbasename属性を設定します.
レスポンスを設定します bundle:
レスキューを検索 bundleの順序: get Message方法にLocaleが設定されている場合、優先的にbasenameを検索する.laguage.country.propertiesファイル.その中のlangageは、Localeに設定されているlangageである. basenameがなければ.laguage.country.propertiesファイルはbasenameを検索します.laguage.propertiesファイル. basenameがなければ.laguage.propertiesファイルは、basename.propertiesファイルを検索します. もしまだgetMesageでdefault情報を伝えておらず、basename.propertiesファイルがない場合、容器はorg.springframe ewark.com ntect.NoSuchMessage Exceptionを投げます. resourceで bundleではプレースホルダを使用します.
レスキューにいてもいいです bundleでは{n}(nは>=0の整数)を使用して、getMessage方法の第二のパラメータの中で、プレースホルダの値を置き換えるためにObject配列を送ることができます.
例:
resource bundle:入力された配列の要素の個数がプレースホルダの個数より少ない場合、容器は配列要素でプレースホルダを順番に置換します.多く出たプレースホルダは{n}の形で戻ります. 伝人の配列の要素の個数がプレースホルダの個数より多い場合、容器は配列要素でプレースホルダを順番に置換します.複数の配列要素は使用されません.
問題
国際化の際には、複数の異なるlocaleのリソースファイルを使用する必要があります.
ソリューション
ApplicationインターフェースはKeyによって異なるLocaleからのレスポンスを提供しています. bundleでテキスト情報を取得する機能です.
(1)まずレスポンスを保証する bundleは正しいフォーマットです.basename[u langage].propertiesは、classipathのルートディレクトリの下に保存されます.
(2)設定ファイルにレスポンスを宣言する bundleのbasename:org.springframe.com.support.Resourceのbasename属性を設定します.
レスポンスを設定します bundle:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="test-messages"/>
</bean>
複数のレスポンスを設定 bundle:<beans>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>format</value>
<value>exceptions</value>
<value>windows</value>
</list>
</property>
</bean>
</beans>
(3)Application Contectインターフェースのget Messageを使用してテキスト情報を取得する:ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
...
String alert = context.getMessage("alert.checkout", null, Locale.US);
System.out.println(alert);
注意レスキューを検索 bundleの順序:
レスキューにいてもいいです bundleでは{n}(nは>=0の整数)を使用して、getMessage方法の第二のパラメータの中で、プレースホルダの値を置き換えるためにObject配列を送ることができます.
例:
resource bundle:
alert.checkout=A shopping cart costing {0} dollars has been checked out at {1}.
ベルン:String alert = context.getMessage("alert.checkout",new Object[] { 4, new Date() }, Locale.US);
注意: