Load resources dynamicallyTag(s): Internationalization
This HowTo lets you select a language and change its GUI to reflect the choice.
We are using a special class, a ListResourceBundle, to store our resources. We have a class per Locale. If a specific resource is not in a specific Locale ListResourceBundle then the default resource is used.
To use a properties file instead of a class to store resources then look at this HowTo.
[InternationalDynamic.java (main program)]
import java.util.*; import java.text.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class InternationalDynamic extends JFrame implements ActionListener { static Locale[] localesSupported = { Locale.US, Locale.FRANCE, Locale.GERMANY }; int localeChoosen = 0; Locale localeCurrent; ResourceBundle rb; ButtonGroup bg; JButton btnQuit; JRadioButton r0, r1, r2; JLabel today; boolean defaultDone = false; public void initLocale(){ localeCurrent = localesSupported[localeChoosen]; this.setLocale(localeCurrent); rb = ResourceBundle.getBundle("ResourcesDynamic", localeCurrent); } public void initText() { setTitle (rb.getString("title")); r0.setText(rb.getString("r0")); r1.setText(rb.getString("r1")); r2.setText(rb.getString("r2")); btnQuit.setText(rb.getString("btnQuit")); Date d = new Date(); MessageFormat mf = new MessageFormat (rb.getString("today"), localeCurrent); today.setText(mf.format(new Object [] { d })); } public void initGUI(){ setLayout(new FlowLayout()); // RADIO buttons bg = new ButtonGroup(); r0 = new JRadioButton(); r1 = new JRadioButton(); r2 = new JRadioButton(); bg.add(r0); bg.add(r1); bg.add(r2); add(r0); add(r1); add(r2); // default RADIO button if (!defaultDone) { String rDef = rb.getString("rDefault"); if (rDef.equals("r0")) r0.setSelected(true); else if (rDef.equals("r1")) r1.setSelected(true); else r2.setSelected(true); defaultDone = true; } else { if (localeChoosen == 0) r0.setSelected(true); else if (localeChoosen == 1) r1.setSelected(true); else r2.setSelected(true); } // QUIT button btnQuit = new JButton(); add(btnQuit); // today label today = new JLabel(); add(today); // r0.addActionListener(this); r1.addActionListener(this); r2.addActionListener(this); btnQuit.addActionListener(this); setSize(400,100); setVisible(true); } public void actionPerformed(ActionEvent ae){ if (ae.getSource() == btnQuit) { System.exit(0); } else if (ae.getSource() == r0) localeChoosen = 0; else if (ae.getSource() == r1) localeChoosen = 1; else if (ae.getSource() == r2) localeChoosen = 2; initLocale(); initText(); pack(); } public static void main(String args[]) { System.out.println("Current Locale : " + Locale.getDefault()); Thread t = new Thread () { public void run() { InternationalDynamic i = new InternationalDynamic(); i.initLocale(); i.initGUI(); i.initText(); i.pack(); } }; SwingUtilities.invokeLater(t); } }
import java.util.*; public class ResourcesDynamic extends ListResourceBundle { public Object [][] getContents() { return contents; } static final Object[][] contents = { { "title", "Example" }, { "r0" , "United States" } , { "r1", "France" } , { "r2" , "Germany"}, { "rDefault" , "r0" }, { "btnQuit" , "Quit"}, { "today" , " (def) {0,date,long}"}, }; }
import java.util.*; public class ResourcesDynamic_en extends ListResourceBundle { public Object [][] getContents() { return contents; } static final Object[][] contents = { { "title", "Example" }, { "r0" , "United States" } , { "r1", "France" } , { "r2" , "Germany"}, { "rDefault" , "r0" }, { "btnQuit" , "Quit"}, { "today" , " (en) {0,date,long}"}, }; }
import java.util.*; public class ResourcesDynamic_fr extends ListResourceBundle { public Object [][] getContents() { return contents; } static final Object[][] contents = { { "title", "Exemple" }, { "r0" , "Etats-Unis" } , { "r1", "France" } , { "r2", "Allemagne" }, { "rDefault", "r1" }, { "btnQuit", "Quitter" }, { "today" , " (fr) {0,date, dd/MM/yyyy}"}, }; }
import java.util.*; public class ResourcesDynamic_de extends ListResourceBundle { public Object [][] getContents() { return contents; } static final Object[][] contents = { { "title", "Beispiel" }, { "r0" , "Vereinigte Staaten" } , { "r1", "Frankreich" } , { "r2", "Deutschland" }, { "rDefault", "r2" }, { "btnQuit", "verlassen"}, { "today" , " (de) {0,date,dd.MM.yyyy}"}, }; }