fr
locale
overrides the month names as "janvier" etc, ; the Canadian French locale
fr_CA
further overrides the currency symbol (from nothing
to "$") and the date/time patterns
JAVA_SRC/sun/text/resources
such as
LocaleElements.java
, LocaleElements_fr.java
and LocaleElements_fr_CA.java
ResourceBundle
class
MyAppResource extends ResourceBundle
); or
PropertyResourceBundle
; or
ListResourceBundle
ResourceBundle
base name is a name with all locale
tags left off. e.g. MyApp_fr_FR_euro
, MyApp_fr_FR
,
MyApp_fr
and MyApp
all have the same basename,
MyApp
ResourceBundle
's are created by static methods that take the
basename and add locale info, trying to find a class that matches
MyApp
in the fr_CA
locale and the default locale is en_US
, it will
try to load the following
MyApp_fr_CA.class
MyApp_fr_CA.properties
This can only be handled by a properties subclass!
MyApp_fr.class
MyApp_fr.properties
MyApp_en_US.class
MyApp_en_US.properties
MyApp_en.class
MyApp_en.properties
MyApp.class
MyApp.properties
MyApp
OKButtonLabel=Ok CancelButtonLabel=Cancel LogoFile=company_logo.gif
baseName_locale.properties
MyApp_en.properties
OKButtonLabel=Ok
CancelButtonLabel=Cancel
LogoFile=company_logo.gif
MyApp_en_AU.properties
OKButtonLabel=She'll be right mate
CancelButtonLabel=Get lost
There is no need to repeat the LogoFile
since it can be found
by the klunky version of inheritance used by resource bundles
ResourceBundle.getgetBundle(String baseName, Locale locale)
(plus variations) can be used to get a bundle
ResourceBundle.getObject(String key)
or
ResourceBundle.getString(String key)
ResourceBundle.getKeys()
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.Locale;
public class GetBundle {
public static void main(String[] args) {
Locale locale = new Locale("en", "US");
ResourceBundle bundle = ResourceBundle.getBundle("MyApp", locale);
Enumeration keys = bundle.getKeys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = bundle.getString(key);
System.out.println("Key: " + key + ", Value: " + value);
}
}
}
ResourceBundle
or
ListResourceBundle
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.Locale;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
public class UseBundle {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("MyApp");
String okLabel = bundle.getString("OKButtonLabel");
String cancelLabel = bundle.getString("CancelButtonLabel");
String[] choices = {okLabel, cancelLabel};
Object[] messages = new Object[1];
messages[0] = "Do it";
JOptionPane pane = new JOptionPane(messages,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null,
choices, choices[0]);
JDialog dialog = pane.createDialog(null, "hi");
dialog.setVisible(true);
}
}
Run by java -Duser.language=en -Duser.country=AU UseBundle