言語リソースファイルの使い方
- Strutsで言語リソースとしてpropertiesファイルを使用する方法
リソースファイルを用意
- hogehoge.properties, hogehoge_ja_JP.propertiesというファイルをsrc/resources以下に用意する。
- 内容は以下のとおり
- hogehoge.properties
hello.message1=hello
bye.message2=bye
bye.message3={0}, bye
- hogehoge_ja_JP.properties
hello.message1=こんにちは
bye.message2=さいなら
bye.message3={0} よ、さよなら
struts-config.xml
JSPからのアクセス方法
Actionクラスからのアクセス方法
- アクションクラスの中で以下のように使用する
- ブラウザの言語設定が日本語の場合は、hello.equals("こんにちは"), bye.equals("さいなら"), bye2.equals("hogeよ、さよなら")となり、それ以外の場合はhello.equals("hello"), bye.equals("bye"), bye2.equals("hoge, bye")となる。
MessageResources resources = getResources(request, "hogekey");
String hello = resources.getMessage("hello.message1");
String bye = resources.getMessage("bye.message2");
String bye2 = resources.getMessage("bye.message3", "hoge");
|