Localization +Problem Instantiating

Hi,

I’m really blocked in a problem, i’m trying to use localization and internationalization in my vaadin application.
this the code that causes problem.
lc = new ArrayList();
lc.add(“FR”);
lc.add(“EN”);
cbLanguage = new ComboBox(null, lc);
cbLanguage.setWidth(“50”);
cbLanguage.setImmediate(true);
cbLanguage.addListener(new Property.ValueChangeListener() {

        private static final long serialVersionUID = 1L;

        @Override
        public void valueChange(ValueChangeEvent event) {
            language = event.getProperty().toString();
            if (language.equals("FR")) {
                captions = ResourceBundle.getBundle("Labels", new Locale("fr", "FR"));
                getWindow().showNotification(captions.toString());
            } else if (language.equals("EN")) {
                captions = ResourceBundle.getBundle("Labels", new Locale("en", "US"));
                getWindow().showNotification(captions.toString());
            }
        }
    });

In fact, the getWindow.showNotification show me that the “captions” object is really instiantiated. (i use it just for testing if the captions object is really instantiated)
But when i try to use it later i got NullPointerException.

speciflabel = new Label(captions.getString(“lbSpecification”));

Can anyone help me!!

regards,
jazia

Do you initialize the “captions” member somewhere? If you initialize it only in the ValueChangeListener, it will be null until the user selects an item and the listener is called.

You don’t need the if-conditionals in language selection. You should put language IDs like “fr_FR” and “en_US” in the combobox as item IDs, and set the item captions of those items to “French” and “English”, respectively. Then, in the ValueChangeListener, use the language ID directly in new Locale(itemId).

Also, you shouldn’t use strings as resource IDs, as it’s really easy to typo them and then you’ll get a MissingResourceException.

Language selection is demonstrated in
LoginScreen.java
(and the application class) in the Gas Diary example. You can run the demo
here on-line
to see how it works.

There’s also this new
I18N4Vaadin
add-on, which helps with internationalization. It can switch the language without having to reconstruct the UI.

Hi,

Thanks Marko Gronroos for your help. it works well with the vaadin add-on!!!

:slight_smile: