Change language, with a Button to reload page with a query.

I have a bilingual website, I would like the user to have the abolity to swith language and direction with a button click, the URL will have a QueryParameter that holds the language key lang=en

The BeforeEvent is called to check for the parameter and construct the layout.

if I use


I cant change the query,
if I used

ui.navigate(path, new QueryParameters(q));

I dont get a full repload to reconstruct the menus!
I appreciate your help,

Code for Button,

Button nav = new Button(getT("change-lang"), (e) -> {
            System.err.println("old: "+Application.getLocaleSelected());
            System.err.println("new: "+Application.getLocaleSelected());
            getUI().ifPresent(ui-> {
                String path=UI.getCurrent().getInternals().getActiveViewLocation().getPath();
                Map<String, List<String>> q=new HashMap<>(UI.getCurrent().getInternals().getActiveViewLocation().getQueryParameters().getParameters());
                q.put(MyParameters.LANG, List.of(Application.getOtherLocale().getLanguage()));
                ui.navigate(path, new QueryParameters(q));

Actually the preferred mechanism in Vaadin is to change Locale programmatically. That will trigger LocaleChangeEvent and you can implement LocaleChange listener in all the views that have language elements.

Well, that means I have to have a separate method to update text?? :thinking:

Which means components must be field variables? To reference them back … too much code

Most of the components are labels, text fields , html codes, helper texts, and these components are added in constructor or beforeenter, I don’t see why I have to have them as field variables in my class to update them with a locale change.

Personally, I would go with Tatu’s recommendation as well.

But you could also hack it like this:

I think I tested this in the above code, reload() does not take the new parameters as inputs.

This is what I use for changing the language.

        e -> SessionUtils.changeSessionLanguage(new Locale("es", "US")));
        e -> SessionUtils.changeSessionLanguage(new Locale("en", "US")))
public static void changeSessionLanguage(Locale locale) {


    // Create a new cookie
    Cookie myCookie = new Cookie("myPreferredLocale", locale.getLanguage());

    // Set the cookie path.

    // Save cookie



It works great as long as you use getTranslation("") wherever you display text.