0
0

Java Gold 例題 MessageFormat, ResourceBundle

Last updated at Posted at 2024-08-11

以下のプロパティファイルがあります。
MyResources_en_US.properties

message = Hello {0}. My name is {1}. I want to drink {2}. Can you tell me where I can buy it? thank you {3}.

次のコードのinsert codeにどのコードを入れると、
次の出力結果が表示される選択肢はどれでしょうか。

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

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

		Object[] paramsUS = {
				"Jhon",
				"Kate",
				"water",
				"see you again"
		};
		
		ResourceBundle bundleUS =
          ResourceBundle.getBundle("MyResources", Locale.US);
		String messageUS = /* insert code */;
		System.out.println(messageUS);
	}
}

出力

Hello Jhon. My name is Kate. I want to drink water. Can you tell me where I can buy it? thank you see you again.
  1. MessageFormat.format(bundleUS, paramsUS)
  2. MessageFormat.format(bundleUS.get("message"), paramsUS)
  3. MessageFormat.format(bundleUS.getString("message"), paramsUS)
  4. MessageFormat.format(paramsUS, bundleUS.getString("message")
  5. MessageFormat.format(paramsUS, bundleUS)
  6. MessageFormat.format(paramsUS, bundle.get("message")
  7. 該当なし

3: MessageFormat.format(bundleUS.getString("message"), paramsUS)

ResourceBundle bundleUS = ResourceBundle.getBundle("localizeAndFormat.format.MyResources", Locale.US);

ResourceBundle.getBundle は、指定されたロケールに対応するプロパティファイル MyResources_en_US.propertiesを読み込みます。

String messageUS = MessageFormat.format(bundleUS.getString("message"), paramsUS)

MessageFormat.format(String, Object[]) は、テンプレートに渡されたパラメータを埋め込み、完成したメッセージ文字列を生成します。

bundleUS.getString("message") は、プロパティファイルから message の値を取得します。この値は、パラメータを含むメッセージテンプレートです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0