I’m making a test application in Vaadin (because we plan on moving ours to Vaadin soon), and I need internationalization.
I’ve found the I18N4Vaadin addon, and it sounds like it will be quite useful.
However, I don’t seem to be able to get it to work as advertised, namely, to be able to change the locale for the app without restarting or reconstructing it. I’m sure it has something to do with how I’m doing it, so I’m looking for some pointers.
Using the example code found on the I18N4Vaadin addon page I’ve extended VerticalLayout and implemented the I18N interfaces:
public class I18NLayout extends VerticalLayout implements I18NComponent, I18NListener {
private static final long serialVersionUID = 1L;
private final I18NComponentSupport support = new I18NComponentSupport(this);
public I18NLayout() {
super();
}
public I18N getI18N() {
return support.getI18N();
}
public void setI18N(I18N i18n) {
support.setI18N(i18n);
}
public void attach() {
super.attach();
getI18N().addListener(this);
updateLabels();
}
public void detach() {
getI18N().removeListener(this);
super.detach();
}
public void localeChanged(I18N sender, Locale oldLocale, Locale newLocale) {
// TODO why get sender?
if (null != newLocale) {
if (null == oldLocale || !oldLocale.getDisplayLanguage().equals(newLocale.getDisplayLanguage() ) ) {
updateLabels();
}
}
}
protected void updateLabels() { } // overridden in subclasses
}
I then extend that class to create my pages, and override the updateLabels() method on each of the pages. Here I’m just showing the pertinent methods, handleLanguageSelection(), which gets the new locale and calls localChanged(), and my overridden updateLabels() function:
private void handleLanguageSelection() {
String selectedLanguage = languageSelect.getValue().toString();
Locale oldLocale = getI18N().getCurrentLocale();
Locale newLocale = Locale.ENGLISH;
if (selectedLanguage.equals(englishLanguageString) ) { // English was selected
// default is english and is already set... in other words, do nothing.
} else if (selectedLanguage.equals(dutchLanguageString) ) { // Dutch was selected
newLocale = new Locale("nl", "NL");
} else { // TODO add languages
// should not get here
System.err.println("I18nTestPageLayout::Unexpected language choice: " + selectedLanguage );
}
localeChanged(getI18N(), oldLocale, newLocale);
//this.requestRepaintAll();
}
protected void updateLabels() {
if (null == getI18N() ) {
ResearchLabApplication rla = (ResearchLabApplication)this.getApplication();
setI18N( rla.getI18N() );
}
// Page Title
pageTitleLabel.setValue(getI18N().getMessage(PAGE_ID + ".PageTitle") );
pageTitleLabel.setVisible(false);
pageTitleLabel.setVisible(true);
// Language Selection menu
languageSelect.setCaption(getI18N().getMessage(PAGE_ID + ".Language") );
}
Oh yeah, and I instantiate an I18N in my application class, and pass that to my page layout thus:
public I18NTestPageLayout(I18N i18n) {
super();
setI18N(i18n);
}
If what I’ve shown here isn’t sufficient to show you what I’ve done, please let me know.
As mentioned, the UI does not update when updateLabels() is called. I have verified that I can initialize the Locale to different languages, and the internationalization works then, but I can’t seem to get it to change on the fly.
What am I missing?
Thank you for any help,
Jay