忍者ブログ
よくわからないことを調べて解説してみるブログ。

2024

0420
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

2015

0531
・ResourceBundleは、国際化に対応したPropertiesクラス
・ResourceBundleはnew でインスタンス化できない
・ResourceBundle#getBundle等を使う。
 このメソッドの実行時には、パス付きプロパティファイル名(拡張子なし)を指定する。
 引数にLocaleを指定可能であり、指定のLocaleに応じたプロパティファイルを取得する。
 取得時のファイル名のルールなどはJavaDoc参照されたい(ResourceBundle#getBundle)。
・なお、プロパティファイルからの取得の場合、暗黙的にPropertyResourceBundleが返却されている(クラスファイルからの取得の場合は、ListResourceBundleが返却される)。
・PropertyResourceBundle について仕様を把握したうえで使うべきである。

サンプルソース
フォルダ構成
src/may20150531
┗ ResourceBundleTest.java
  /resourcebundle
  ┗・sampleprop_jp_JA.properites
   ・sampleprop_en_US.properties

sampleprop_jp_JA.properites
name=sampleProp
key1=20150531
lang=Japan
sampleprop_en_US.properties
name=sampleProp
key1=20150531
lang=English
ResourceBundleTest.java

package may20150531;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleTest {
	public static void main(String[] args) {

		System.out.println(Locale.getDefault());
		System.out.println("--------------------------");
		System.out.println("ResourceBundle.getBundle(\"may20150531.resourcebundle.sampleprop\");");

		ResourceBundle bundle = ResourceBundle.getBundle("may20150531.resourcebundle.sampleprop");
		Enumeration keys = bundle.getKeys();

		while (keys.hasMoreElements()) {
			System.out.println(bundle.getString(keys.nextElement()));
		}
		System.out.println("--------------------------");
		System.out.println("ResourceBundle.getBundle(\"may20150531.resourcebundle.sampleprop\", Locale.JAPAN);");

		bundle = ResourceBundle.getBundle("may20150531.resourcebundle.sampleprop", Locale.JAPAN);
		keys = bundle.getKeys();

		while (keys.hasMoreElements()) {
			System.out.println(bundle.getString(keys.nextElement()));
		}
		System.out.println("--------------------------");
		System.out.println("ResourceBundle.getBundle(\"may20150531.resourcebundle.sampleprop\", Locale.US);");
		bundle = ResourceBundle.getBundle("may20150531.resourcebundle.sampleprop", Locale.US);
		keys = bundle.getKeys();

		while (keys.hasMoreElements()) {
			System.out.println(bundle.getString(keys.nextElement()));
		}

	}
}


実行結果

ja_JP
--------------------------
ResourceBundle.getBundle("\may20150531\resourcebundle\sampleprop");
20150531
sampleProp
Japan
--------------------------
ResourceBundle.getBundle("\may20150531\resourcebundle\sampleprop", Locale.JAPAN);
20150531
sampleProp
Japan
--------------------------
ResourceBundle.getBundle("\may20150531\resourcebundle\sampleprop", Locale.US);
20150531
sampleProp
English

拍手[0回]

PR
Post your Comment
Name:
Title:
Font:
Mail:
URL:
Comment:
Pass: Vodafone絵文字 i-mode絵文字 Ezweb絵文字
プロフィール
HN:
たんてーくん
性別:
非公開
フリーエリア
最新CM
[09/25 http://2017.bblbuy.com]
[09/24 http://www.japanform.com]
[09/23 http://www.japanform.com]
[09/22 http://www.japanform.com]
[09/21 http://2017.bblbuy.com]
ブログ内検索
忍者ブログ [PR]
* Template by TMP