以下のプロパティファイルがあります。
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.
- MessageFormat.format(bundleUS, paramsUS)
- MessageFormat.format(bundleUS.get("message"), paramsUS)
- MessageFormat.format(bundleUS.getString("message"), paramsUS)
- MessageFormat.format(paramsUS, bundleUS.getString("message")
- MessageFormat.format(paramsUS, bundleUS)
- MessageFormat.format(paramsUS, bundle.get("message")
- 該当なし
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
↓
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
の値を取得します。この値は、パラメータを含むメッセージテンプレートです。