Java by API/java.util/ResourceBundle
Версия от 17:43, 31 мая 2010; (обсуждение)
ResourceBundle: getBundle(String baseName)
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class MainClass {
public static void main(String args[]) throws Exception {
ResourceBundle resources;
try {
resources = ResourceBundle.getBundle("MyData");
System.out.println(resources.getString("Hi"));
} catch (MissingResourceException mre) {
System.err.println("MyData.properties not found");
}
}
}
//File: MyData.properties
/*
Hi = Hello World!
*/
//File: MyData_fr.properties
/*
Hi = Salut tout le monde!
*/
ResourceBundle: getBundle(String baseName, Locale locale)
import java.util.Locale;
import java.util.ResourceBundle;
public class Main {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("MessagesBundle", Locale.UK);
System.out.println("Message in " + Locale.UK + ": " + bundle.getString("greeting"));
Locale.setDefault(new Locale("in", "ID"));
bundle = ResourceBundle.getBundle("MessagesBundle");
System.out.println("Message in " + Locale.getDefault() + ": " + bundle.getString("greeting"));
}
}
Example of our resource bundle files,
MessagesBundle_en_GB.properties
greeting=Hello, how are you?
MessagesBundle_in_ID.properties
greeting=Halo, apa kabar?
ResourceBundle: getString(String key)
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class MainClass {
public static void main(String args[]) throws Exception {
ResourceBundle resources;
try {
resources = ResourceBundle.getBundle("MyData");
System.out.println(resources.getString("Hi"));
} catch (MissingResourceException mre) {
System.err.println("MyData.properties not found");
}
}
}
//File: MyData.properties
/*
Hi = Hello World!
*/
//File: MyData_fr.properties
/*
Hi = Salut tout le monde!
*/