Search code examples
javalocalizationinternationalizationresourcebundlewindowbuilder

WindowBuilder: change language at runtime


So I have this Java project (JDesktopPane with JInternalFrames) with WindowBuilder and now I am to translate the whole program. Every time a JInternalFrame is opened from the JDesktopPane Menu, a new JInternalFrame will be instanciated.

Task: the user should be able to switch between various languages at runtime (the language must not change directly, it would be sufficient on re-opening the JInternalFrame. Right now I make use of the ResourceBundle in every JInternalFrame:

private static ResourceBundle BUNDLE;

An set the text with:

lblText = new JLabel(BUNDLE.getString("TestWindow.lblText.text")); //$NON-NLS-1$

In order to switch between the languages, I have a Menu in the JDesktopPane, which sets for example

language = "en";

Upon creating a new Window, the JInternalFrame gets a reference to the MainWindow and reads this property and sets it in the JInternalFrame:

BUNDLE = ResourceBundle.getBundle("TestWindow.messages_"+ mainWindow.language); //$NON-NLS-1$

The problem: WindowBuilder interferes this and sets

private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("TestWindow.messages"); //$NON-NLS-1$

every Time I open the Window in the design view. Furthermore, I get plenty of errors in the Editor Errors window of WindowBuilder which is because the Editor can't resolve "TestWindow.messages_"+ mainWindow.language, which is clear to me.

Is there any way of dealing with this in a more correct way? I wasn't able to figure something correct out within WindowBuilder so I came up with this hacky solution.

Thanks, king-al


Solution

  • Wouldn't

    Locale.setDefault(new Locale("en")); // Locale.ENGLISH
    

    be sufficient? Simply getting the root bundle will get the correct locale bundle.