Providing a Multilingual GUI

When generating code SpeedJG strictly follows the MVC approach by separating the GUI (view) code from the controller code and the model code, which is up to you as the developer. To take advantage of this separation, we will show how to easily implement a multilingual application by passing an entire GUI to a translator class that adjusts all text properties according to the language the user prefers.

Multilingual GUI

To achieve our goals we will develop a GuiTranslator class that reads all text properties to be assigned from an XML file, stores them locally, and translates our GUI on demand.

The structure of this file corresponds to the task the translator has to accomplish. On the top level we define the elements for the GUIs to be translated. This may be the name of the top-level element / container (e.g. JFrame, JDialog, etc.) hosted by the GUIObject (view) to be translated, or a name you internally assign to the GUIObject. On the next level we define the items of the GUIObject to be translated and on the lowest level the properties of the items that are to be affected.

The names of these property elements signalize to the translator to accomplish a particular task and are defined as:

  • accelerator
    to set the keyboard accelerator of a JMenuItem;
  • items
    to set the items of a JComboBox;
  • mnemonic
    to set the mnemonic (a single underline below the character that represents the shortcut) of an AbstractButton (JButton, JToggleButton, JCheckBox, JRadioButton, JMenuItem, JMenu, JCheckBoxMenuItem, JRadioButtonMenuItem);
  • tabtitle
    to set the name of a component layed out on a JTabbedPane;
  • text
    to set the text of a JLabel or an AbstractButton (JButton, JToggleButton, JCheckBox, JRadioButton, JMenuItem, JMenu, JCheckBoxMenuItem, JRadioButtonMenuItem);
  • title
    to set the title of a JDialog or JFrame;
  • tooltip
    to set the tooltip (the small window of text that pops up when the user moves the mouse over the target component) of a JComponent;

and the attribute values of these property elements hold the specific language texts (see below for the fraction of the XML translation file used in this example).

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
   <items de="Frankreich|Deutschland|USA|..." en="France|Germany|USA|..." />
   <accelerator de="C" en="C" />
   <mnemonic de="k" en="c" />
   <text de="Kopieren" en="Copy" />
   <title de="Mehrsprachige Anwendung" en="Multilingual Application" />
   <tooltip de="Kopieren" en="Copy" />

As a GUIObject generated by SpeedJG provides access to all contained components by their name it's an easy task for the translator class to scan the translation repository file and set the properties according to the defined entries.

If you want to see how all this blends in and wish to experiment with an example program, store the following files into a directory of your choice and subsequently compile them:

Multilingual GUI Example Application Files Superclass of all view classes generated by SpeedJG. Main class generated by SpeedJG and modified for this example. Generated by SpeedJG. Translator class for setting translated text properties of a view.
GuiTranslationRepository.xml XML file containing all text properties to be set by the translator.
jlfgr-1_0.jar Java look and feel Graphics Repository. The buttons of this example application display images contained in this .jar file. Download and un-zip the file and place the jlfgr-1_0.jar file on your CLASSPATH.