Java Resource Bundles


Locale data


ResourceBundle


ResourceBundle criticisms


PropertyResourceBundle


Getting resource bundles


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);
	}
    }
}
    


Resources of objects

Use


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


Jan Newmarch <jan@newmarch.name>
Last modified: Fri Mar 11 10:53:00 EST 2005
Copyright © Jan Newmarch, Monash University, 2007
Creative Commons License This work is licensed under a Creative Commons License
The moral right of Jan Newmarch to be identified as the author of this page has been asserted.