Skip to main content

Internationalization at Runtime - How to do that?

3 replies [Last post]
remast
Offline
Joined: 2006-04-27

Hi All,

I need to make it possible to change the language at runtime. It is a pure swing application.

How can I do that?

Right now I have the problem that all default texts that are resolved by the UIManager are in the language used at startup.

Any hints ?

Thanks,
remast

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
remast
Offline
Joined: 2006-04-27

Hi,

thanks a lot!! That did indeed help me.

Yet still there are some open questions. The post works for the swing stuff yet not for titles of actions and the like.

Is there a way to reload these without actually creating new instances by hand?

Thanks,
remast

pietblok
Offline
Joined: 2003-07-17

Hi remast,

Unfortunately I'm not aware of an easy way to automate the switching to other values, for example setTitle etc, So you have in some way to centralize those methods where you can handle them. You might introduce listeners that set these texts. The UIManager fires a PropertyChangeEvent when the LAF changes (propertyname: "lookAndFeel") and allows you to register listeners.

Piet

pietblok
Offline
Joined: 2003-07-17

Hi remast,

You may set a new instance of the LAF and update your UI. The Swing provided values will be refreshed to the current Locale. A very simple example:

[code]
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Comparator;
import java.util.Locale;
import java.util.TreeSet;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DynamicLanguage {

public static Locale[] getCommonLocales() {
Collection locales = new TreeSet(
new Comparator() {

@Override
public int compare(Locale o1, Locale o2) {
return o1.getDisplayName(o1).compareTo(
o2.getDisplayName(o2));
}
});
for (Field field : Locale.class.getFields()) {
if (Locale.class.isAssignableFrom(field.getType())) {
try {
locales.add((Locale) field.get(null));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
locales.add(Locale.getDefault());
return locales.toArray(new Locale[locales.size()]);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JFileChooser());
frame.setJMenuBar(new JMenuBar());
JMenu menu = new JMenu("Locales");
frame.getJMenuBar().add(menu);
ButtonGroup group = new ButtonGroup();
for (final Locale locale : getCommonLocales()) {
JCheckBoxMenuItem item = new JCheckBoxMenuItem(
new AbstractAction(locale.getDisplayName(locale)) {

private static final long serialVersionUID = 1L;

{
this.putValue(Action.SELECTED_KEY, locale
.equals(Locale.getDefault()));
}

@Override
public void actionPerformed(ActionEvent e) {
setLocale(locale, frame);
}
});
group.add(item);
menu.add(item);
}
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public static void setLocale(Locale locale, final Window... windows) {
Locale.setDefault(locale);
System.setProperty("user.language", locale.getLanguage());
System.setProperty("user.country", locale.getCountry());
System.setProperty("user.variant", locale.getVariant());
try {
/*
* Force setting of a new instance of the current LAF.
*/
UIManager.setLookAndFeel(UIManager.getLookAndFeel().getClass()
.getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
for (Window window : windows) {
SwingUtilities.updateComponentTreeUI(window);
}
}
});
}

}
[/code]

Piet