Search code examples
javaeclipsepropertiesinternationalizationresourcebundle

Internationalization in Java Error


I am learning Java, our teacher told us an internationalization example in netbeans, he created three properties file for different language ( in netbeans u can easily create properties file ), so i was trying this at home using eclipse, now i created 3 properties file exactly in the same folder our teacher created ( where .java files were ) i did the same code he did, but i got an error, then i tried to run the netbeans project ( which i took it from our teacher's computer ) in netbeans, it gave me the same error, now i think coding is fine but there is some error in properties file

here is the code

package example;


import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Locale;
import java.util.ResourceBundle;


public class MyFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    /**
     * 
     */
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyFrame frame = new MyFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MyFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 156);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        final JLabel lbl = new JLabel("My Internationalization Example");
        lbl.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lbl.setBounds(106, 21, 204, 17);
        contentPane.add(lbl);

        JRadioButton rdbtnEnglish = new JRadioButton("English");
        rdbtnEnglish.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                Locale lang = new Locale("en" , "US");
                ResourceBundle message = ResourceBundle.getBundle("src/example/MessageBundle_en_US", lang);
                lbl.setText(message.getString("LangValue"));
            }
        });
        rdbtnEnglish.setBounds(42, 59, 109, 23);
        contentPane.add(rdbtnEnglish);

        JRadioButton rdbtnFrance = new JRadioButton("France");
        rdbtnFrance.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    Locale lang = new Locale("fr" , "FR");
                    ResourceBundle message = ResourceBundle.getBundle("src/example/MessageBundle_fr_FR", lang);
                    lbl.setText(message.getString("LangValue"));
                }
        });
        rdbtnFrance.setBounds(153, 59, 142, 23);
        contentPane.add(rdbtnFrance);

        JRadioButton rdbtnGerman = new JRadioButton("German");
        rdbtnGerman.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                Locale lang = new Locale("de" , "DE");
                ResourceBundle message = ResourceBundle.getBundle("src/example/MessageBundle_de_DE", lang);
                lbl.setText(message.getString("LangValue"));
            }
        });
        rdbtnGerman.setBounds(297, 59, 109, 23);
        contentPane.add(rdbtnGerman);


        ButtonGroup myGroup = new ButtonGroup();
        myGroup.add(rdbtnEnglish);
        myGroup.add(rdbtnFrance);
        myGroup.add(rdbtnGerman);

        rdbtnEnglish.setSelected(true);




    }

}

here is the stuff in all three properties file

MessageBundle_en_US.properties

LangValue = My Internationalization Example

MessageBundle_de_DE.properties

LangValue = Meine Internationalisierung Beispiel

MessageBundle_fr_FR.properties

LangValue = Mon internationalisation Exemple

now this is the folder where my properties and java file is

D:\Documents\Work\Eclipse\InternationalizationExample\src\example

here is a screen shot for a better view

enter image description here

enter image description here

now when i run, my program runs, but when i select the options , it gives me this error which i googled for straight 3 hours and tried everything from changing the location, folders etc

enter image description here

and when i click the other option BOOM , i get an error

enter image description here

and the error is

Exception in thread "AWT-EventQueue-0" java.util.MissingResourceException: Can't find bundle for base name src/example/MessageBundle_fr_FR, locale fr_FR at java.util.ResourceBundle.throwMissingResourceException(Unknown Source) at java.util.ResourceBundle.getBundleImpl(Unknown Source) at java.util.ResourceBundle.getBundle(Unknown Source) at example.MyFrame$3.actionPerformed(MyFrame.java:75) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

for more help, i've upload my whole eclipse project into this rar

http://www.mediafire.com/download/yxnsrch41qhcxuw/InternationalizationExample.rar


Solution

  • Change your getBundle statements from:

    ResourceBundle message = ResourceBundle.getBundle("src/example/MessageBundle_en_US", lang);
    

    to:

    ResourceBundle message = ResourceBundle.getBundle("example/MessageBundle", lang);