Reload page without ending a session

Hey

I’m developing a grails application with the Java framework Vaadin. The tool should support two languages, English and German.
I created two buttons to switch from one language to the other. For the translation I use the i18n add-on and to change the language I use Application.setLocale() and that works perfectly.
But my question is how to reload the page with the new language?
I’ve tried to refresh the application with the Application.close()- function but the function close() ends the user session and all the datas are deleted (Also the Object Locale with the language selection).
So, has anyone an idea how to reload the page without ending the session?

regards

Elia

I’m only familiar with
AppFoundation’s
i18n implementation, so I’m going to speak more generally about i18n in Vaadin applications.

If you manually click on refresh in your browser, does that actually change all the i18n strings in your application? If so, you can achieve this by calling requestRepaintAll() for your application. Actually, if everything is implemented correctly, you shouldn’t need to call that method manually - ever.

However, from experience, I guess that it won’t work, because repainting (refreshing) the application will not set new captions for your components. Due to Vaadin’s statefull nature, changing the language on-the-fly in a Vaadin application can be quite hard to implement. If you don’t mind losing the application state, then the quick-and-dirty way would be to just create a new instance of your main window and repopulate it. This means, that all the components will be recreated, their captions and values reset and thus the strings will be (hopefully) set using the correct language.

Probably, a more suitable way to do this, without losing the application state, would be to create an interface for components which have i18n string within them. For example,

public interface I18n {
   public void updateI18nStrings();
}

Have your layouts/components implement this intercae

public class MyView extends VerticalLayout implements I18n {
   private TextField someField;

   private MySubView subView;

   .....

   public void updateI18nStrings() {
      // Update all i18n for child components
      someField.setCaption(...<get language specific caption>....)
      // Call the same method recursively for child components
      subView.updateI18nStrings();
  }
}

Now if you implement this properly, you should be able to switch the language for the entire application just by calling updateI18nStrings() for your main window. Of course, this requires you to implement the method in quite many places. It might also be useful to create a helper method which will go through the component tree recursively and look for components with the i18n interface. It should be doable, but it won’t be easy.

Hei Kim Leppänen

Many thanks for your answer. I found a similar solution.
I wanted to know if there is a easier solution where you can updating all components with one function.
But if it is the only way I will do this by updating each component in my view.

Regars

Elia

Hi,

Ages ago
I published some (Apache2 licensed) source code
that would effectively traverse the entire component tree.

I essentially forked this from my work project (with permission), but I haven’t been maintaining it - it’s all standard vaadin, though, and will still work.

In our Window class, we’ve added this method

public Iterable<? extends Component> getAllComponents() {
    return ComponentIterables.componentTreeBreadthFirst(this);
  }

We’ve combined this with some code from google guava-libraries, so that we can simply do


for (LocaleChangeListener localeChangeListener : Iterables.filter(getAllComponents(), LocaleChangeListener.class)) {
      localeChangeListener.localeChanged(oldLocale, newLocale);
    }

Feel free to copy/fork/use this code in anyway you see fit. Or not!

Cheers,

Charles.