Dynamically change Locale of UI

Im am using Vaadin 7.1 and need to be able to change the form / screen / UI Locale dynamically (user can select the preferred form locale). By changing the form locale i mean changing field names / captions, window title, user messages / notification, and so on.
This is not trivial with Vaadin afaik. What is the best practice for this use case? Any advice for external libraries / design approach will be highly appreciated!

Ronald

Hi,

you’re correct - it’s not that easy to do it on the fly. The usual (easiest/quickest/cheapest) approach for changing the locale is to allow the user to either select it on login and apply it starting from there, or allow the user to set the locale in some user settings view and then it will be applied after a relogin.

If you do need this to work on the fly you might want to look at the i18n add-on:
http://vaadin.com/directory/#addon/i18n4vaadin

Another option which I’ve seen used in projects is just a custom implementation so you can exactly fit it to your needs.

-tepi

In my App I have a similar requirement.

I achieved this with storing the Locale the user selected in the User-Session and wrapped methods which set e.g. captions to use the locale in the session to display the right language.

@Teppo. I have looked at the i18n4vaadin add-on, but “on the fly updating the components” (what I want to achieve) is
not supported: “you have to remember to update all your UI components since this is not something I18N4Vaadin can do automatically.” Secondly, it does not fit well in our application architecture (we use declarative/XML based layouts).

@Holger. Our approach we are now trying (not finalized): Get a reference to the current UI, walk the component tree and update all String setters with an i18n key by using reflection.

Both thanks! This is valuable input.

This is maybe not what you’d like to hear, but think if it actually is worth implementing. It usually is a lot of work and error prone to try to traverse every component in the whole application. What I usually do is either force a refresh out of the application, or have a separate initialization method in UI which I can call to reinitialize the whole UI. This combined with navigator gets me into the right view with updated captions.

It’s a trade-off between usability and effort. In my opinion the required effort is quite big while the hit on usuability is quite low, and thus I’ve come to the conclusion that reinitializing the UI gives me the most value out of the effort.

Thanks Jens. I agree with the trade-off, but sometimes you are not in the position to make the trade-off :slight_smile:

We finally came up with this:
https://gist.github.com/rmuller/10980706#file-vaadinutils-java

It is reasonable simple and very fast (just about 10-20 ms for some large UI’s).
WARNING: just finished and only tested within one application.

Ronald