Load resources via a resources fileTag(s): Internationalization
With the previous JAVA How-to, the resources were stored in classes. The drawback is when there is a modification, we must recompile the class. With a resources file, we simply add or modify the resource in a special file with a regular text editor. The JDK provides a class, PropertyResourceBundle, to use properties file very easily. In fact, from the programmer's point of view, there is no difference if the resources are stored in classes or properties files.
The ResourceBundle will look first for classes and if not found, it will look for properties files. We don't have to specify filenames, the ResourceBundle will construct the filename using the same mechanism used for classes. The file must have the extension .properties.
The ResourceBundle try to load the properties file from the current classpath.
If the properties are stored in subdirectory , use "." instead of "/".
For example, let's say that the properties file is in a subdirectory called "subdir" (from the current directory). Then you can load the ResourcesDynamic resources file with
ResourceBundle.getBundle("subdir.ResourcesDynamic", localeCurrent);
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("res.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); } }
title=Example r0=United States r1=France r2=Germany rDefault=r0 btnQuit=Quit today=(def) {0,date,long}
title=Example r0=United States r1=France r2=Germany rDefault=r0 btnQuit=Quit today=(en) {0,date,long}
title=Exemple r0=Etats-Unis r1=France r2=Allemagne rDefault=r1 btnQuit=Quitte today=(fr) {0,date, dd/MM/yyyy}
title=Beispiel r0=Vereinigte Staaten r1=Frankreich r2=Deutschland rDefault=r2 btnQuit=Verlassen today=(de) {0,date,dd.MM.yyyy}