Java Swing GUI Internationalization and String Separation using ResourceBundle and Locale

Java Swing GUI  Internationalization and String Separation using ResourceBundle and Locale:

Before proceeding take note of the directory structure of the highlighted part otherwise it won’t work. Or, just fork this repository from Github from this link,

https://github.com/quickgrid/QuickEditor

Output:

java swing internationalization output
java swing internationalization output

Directory Structure:

internationalization directory structure
internationalization directory structure

 


Codes:

Here I have separated all strings to a directory res/locale using ResourceBundle class. But I have only shown Internationalization for the application name using the Locale class to specify which locale it is going to use. Here one of the locale is French another is English. More can be added.

For the menubar Menus I have used ResourceBundle class to separate them for easier modification later. They too can be internationalized using Locale. If the Locale class based codes are deleted here then it will switch to the default properties filename. Note if you cut the ‘locale’ parameter in line 53 then the window name will be “Quick Ide” since it will switch to the default one.

Lastly I have separated the menu creation logic to another class called AddMenu. Note I have only added two Menu but more can be added.

Learn more on Internationalization.

Some important things are highlighted below.

QuickEditor.java
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Created by asif on 8/18/2015.
 */
public class QuickEditor {
    /**
     * variables declaration
     */
    JFrame jFrame;
    JMenuBar jMenuBar;


    /**
     * ResourceBundle variables for internationalization and easier modification
     */
    ResourceBundle appNameResourceBundle;

    /**
     * AddMenu class add new JMenu and JMenuItem to the JMenuBar
     */
    AddMenu addMenu;


    public QuickEditor(){
        prepareResourceBundle();

        jFrame = new JFrame(appNameResourceBundle.getString("ApplicationName"));

        jMenuBar = new JMenuBar();

        addMenu = new AddMenu(jMenuBar);

    }

    private void prepareResourceBundle() {

        /**
         * Delete this portion in case you are just separating string from the code
         * also delete the 'locale' argument in the next portion
         */
        String country = new String("fr");
        String language = new String("Fr");
        Locale locale = new Locale(country, language);

        /**
         * The dot specifies a directory before the file name
         */
        appNameResourceBundle = ResourceBundle.getBundle("res.locale.ApplicationName", locale);
    }

    public void prepareGUI(){
        jFrame.setSize(400, 400);
        jFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
            }
        });

        /**
         * addComponents is a method in AddMenu Class which adds JMenu to JMenuBar
         */
        addMenu.addComponents();

        jFrame.setJMenuBar(jMenuBar);
        jFrame.setVisible(true);
    }

    public static void main(String [] args){
        QuickEditor quickEditor = new QuickEditor();
        quickEditor.prepareGUI();
    }
}

 


AddMenu.java
import javax.swing.*;
import java.util.ResourceBundle;

/**
 * Created by asif on 8/18/2015.
 */
public class AddMenu {
    JMenuBar jMenuBar;
    JMenu fileMenu, viewMenu, helpMenu;
    ResourceBundle menuResourceBundle;

    public AddMenu(JMenuBar jMenuBar) {
        this.jMenuBar = jMenuBar;
        menuResourceBundle = ResourceBundle.getBundle("res.locale.Menus");
        createComponentObjects();
    }

    public void createComponentObjects(){
        fileMenu = new JMenu(menuResourceBundle.getString("FileMenu"));
        viewMenu = new JMenu(menuResourceBundle.getString("ViewMenu"));
    }

    public void addComponents(){
        jMenuBar.add(fileMenu);
        jMenuBar.add(viewMenu);
    }
}

 


Menus.properties
# This is comment
# Created by asif on 8/18/2015.
# I have created some Menu names by default

FileMenu = File
EditMenu = Edit
ViewMenu = View
CodeMenu = Code
ToolsMenu = Tools
WindowMenu = Window
HelpMenu = Help

 


ApplicationName.properties
ApplicationName = Quick IDE

 


ApplicationName_fr_FR.properties
ApplicationName = Rapide IDE
Advertisements