Шрифт:
Рассмотрим пример использования ResourceBundle:
public class MyResource extends ResourceBundle {
private Hashtable res = null;
public MyResource {
res = new Hashtable;
res.put("TestKey","English Variant");
}
public Enumeration getKeys {
return res.keys;
}
protected Object handleGetObject(String key) throws
java.util.MissingResourceException {
return res.get(key);
}
}
public class MyResource_ru_RU extends ResourceBundle {
private Hashtable res = null;
public MyResource_ru_RU {
res = new Hashtable;
res.put("TestKey","Русский вариант");
}
public Enumeration getKeys {
return res.keys;
}
protected Object handleGetObject(String key) throws
java.util.MissingResourceException {
return res.get(key);
}
}
public class Test {
public Test {
}
public static void main(String[] args) {
Test test = new Test;
ResourceBundle rb = ResourceBundle.getBundle("experiment.MyResource",Locale.getDefault);
System.out.println(rb.getString("TestKey"));
rb = ResourceBundle.getBundle("experiment.MyResource", new Locale("ru","RU"));
System.out.println(rb.getString("TestKey"));
}
}
Пример 14.29.
Результатом будет:
English Variant Русский Вариант
Кроме того, следует обратить внимание, что ResourceBundle может хранить не только строковые значения. В нем можно хранить также двоичные данные, или просто методы, реализующие нужную функциональность, в зависимости от локали.
public interface Behavior {
public String getBehavior;
public String getCapital;
}
public class EnglishBehavior implements Behavior {
public EnglishBehavior {
}
public String getBehavior {
return "English behavior";
}
public String getCapital {
return "London";
}
}
public class RussianBehavior implements Behavior {
public RussianBehavior {
}
public String getBehavior {
return "Русский вариант поведения";
}
public String getCapital {
return "Москва";
}
}
public class MyResourceBundle_ru_RU extends ResourceBundle {
Hashtable bundle = null;
public MyResourceBundle_ru_RU {
bundle = new Hashtable;
bundle.put("Bundle description","Набор ресурсов для русской локали");
bundle.put("Behavior",new RussianBehavior);
}
public Enumeration getKeys {
return bundle.keys;
}
protected Object handleGetObject(String key) throws
java.util.MissingResourceException {
return bundle.get(key);
}
}
public class MyResourceBundle_en_EN extends ResourceBundle {
Hashtable bundle = null;
public MyResourceBundle_en_EN {
bundle = new Hashtable;
bundle.put("Bundle description","English resource set");
bundle.put("Behavior",new EnglishBehavior);
}
public Enumeration getKeys {
return bundle.keys;
}
protected Object handleGetObject(String key) throws
java.util.MissingResourceException {
return bundle.get(key);
}
}
public class MyResourceBundle extends ResourceBundle {
Hashtable bundle = null;
public MyResourceBundle {
bundle = new Hashtable;
bundle.put("Bundle description","Default resource bundle");
bundle.put("Behavior",new EnglishBehavior);
}
public Enumeration getKeys {
return bundle.keys;
}
protected Object handleGetObject(String key) throws
java.util.MissingResourceException {
return bundle.get(key);
}
}
public class Using {
public Using {
}
public static void main(String[] args) {
Using u = new Using;
ResourceBundle rb = ResourceBundle.getBundle("lecture.MyResourceBundle", Locale.getDefault);
System.out.println((String)rb.getObject("Bundle description"));