解答例 - 実習課題3 - 6.ロケール/プロパティ/リソースバンドル
(実習課題3)
以下のアプリケーションを作成しなさい。
- プログラムの実行時引数でロケールを指定する。
- 指定したロケールでリソースバンドルを検索し、「hello」に対応する値を表示する事。当然のことながら、その値は「hello」をその言語に翻訳したものにする事。
- 少なくとも3種類以上のロケールに対応したリソースバンドルを作成する事。
解答例
package com.techscore.utility.chapter6.exercise3;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* ResourceBundleExample.java
* TECHSCORE Javaユーティリティ6章 実習課題3
*
* propertiesファイルは、実行するフォルダ、ディレクトリに置くこと
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
public class ResourceBundleExample {
public static void main(String args[]) {
Locale locale = null;
if (args.length == 0) {
System.out.println(
"実行方法:java com.techscore.utility.chapter6.exercise3.ResourceBundleExample 言語名");
System.out.println("言語名は \"en\", \"fr\", \"ja\" から選んでください。");
} else {
locale = new Locale(args[0]);
}
if (locale == null) {
System.out.println("指定された言語が見つかりませんでした、デフォルトのロケールを使用します。");
locale = Locale.getDefault();
}
try {
ResourceBundle bundle =
ResourceBundle.getBundle("com.techscore.utility.chapter6.exercise3.hello", locale);
System.out.println(bundle.getString("hello"));
} catch (Exception e) {
System.out.println("\t" + e.getClass() + " が発生");
}
}
}

