I18N4Vaadin and Reconstructing the UI

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

I’m still hoping for any help anyone can give on this.

Is my post confusing? Please feel free to ask for additional info, or clarification on any point.

If anyone can help, I’d appreciate it.

Thanks,

Jay

I too am only just starting to evaluate vaadin (from echo2)… looks promising so far.
In your example, which component do you expect to see changing ? pageTitleLabel ?

If so, the API seems to suggest that setCaption will trigger a repaint, whereas it doesn’t say that’s the case for setValue… did you mean to call setCaption instead ?

To clarify how painting works in Vaadin:

Any and every method call on Vaadin components that logically changes what it should display triggers a repaint for that component (or sometimes part of a component). Your application code should never need to explicitly trigger a repaint request when using Vaadin. On the other hand, if you simply change your data objects without going through the Vaadin data APIs, Vaadin components are never told that something has changed, so they will show their old content even when you reload the page - the component state is on the server.

Note a separate issue, though: browsers only get updates when they ask for them, so if updates are performed by a background thread on the server, the browser needs to poll the server to get the updates in the repaint queue, or you need to use a push solution. See
this post
for more information.

I don’t know the I18N4Vaadin add-on, but based on a quick look, what you are doing seems ok.

I would recommend putting breakpoints in the methods to see if the locale really gets changed and the updateLabels() gets called, and to see if it sets the correct new values.

One more clarification: A Label has both a caption and a value. The value is the content of the label, whereas the caption is a string that is displayed by the layout that contains the label, most layouts render it above the label itself.

Usually you want a Label to have a value but no caption (null), although there are also cases where setting both can be useful.

Henri,

Thank you for your help (you’ve helped me several times already, on various posts).

In my typical, low-tech debugging style, I did put several print statements in all my functions to verify that it was getting where I expected it to. I was even able to verify that I had the new locale. Reviewing my code, it looks like the one thing I didn’t check was the actual value of the Label. I’ll have to do that.

So, “Status Check”, it seems I am still pretty much where I was- not sure what I’m doing wrong and not sure how to proceed.

Again, anyone can feel free to offer advice, especially say, Petter Holmström (author of this addon), or anyone who has successfully implemented it.

Jay

Matt,

Thank you for your reply. I appreciate you taking the time to attempt to help me.

At this point, I’m feeling like I may try anything. What will it hurt to try setting the caption.

I’ll let you know if it works.

Jay

I’m going to hit this thread again, in hopes that someone will still be able to help me.

I’ve continued playing around with the component, with varying degrees of success (at the moment the internationalization isn’t working at all), but I’ve never been successful in getting the text to change automatically when I select a new language.

Thank you for any help,

Jay

I still haven’t been able to get this to work, quite. Internationalization is very important to my eventual application. I’m still hoping someone can help me with this.

The author of this addon seems to be Petter Holmström. Is it possible you could give some pointers?

Thanks to anyone for help,

Jay

Well, look at that. Suddenly it is working, though I don’t think I changed anything. Perhaps my browser had cached a version that wasn’t working, and now, finally, it’s showing the working one.

Anyway, thanks all for whatever help you gave.

Jay