Localizing Messages with a property-file type resource bundle
/*
MyResources_en.properties:
hello=Hello
bye=Goodbye
MyResources_fr.properties:
hello=Bonjour
bye=Au Revoir
A base name and desired locale is specified.
The system looks in the classpath for a file whose name matches one of the following patterns in order:
basename_locale.properties
basename.properties
basename_defaultLocale.properties
Given the base name MyResources, a locale of fr, and a default locale of en, the system first looks for
MyResources_fr.properties
MyResources.properties
MyResources_en.properties
*/
import java.util.Locale;
import java.util.ResourceBundle;
public class Main {
public static void main(String[] argv) throws Exception {
String baseName = "MyResources";
ResourceBundle rb = ResourceBundle.getBundle(baseName);
String key = "hello";
String s = rb.getString(key); // Hello
System.out.println(s);
key = "bye";
s = rb.getString(key); // Goodbye
// Get the resource bundle for a specific locale
rb = ResourceBundle.getBundle(baseName, Locale.FRENCH);
key = "hello";
s = rb.getString(key); // Bonjour
System.out.println(s);
key = "bye";
s = rb.getString(key); // Au Revoir
System.out.println(s);
}
}
Related examples in the same category