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

UI.getCurrent().getPage().reload();

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());
            Application.setLocale(Application.getOtherLocale());
            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));
                //UI.getCurrent().getInternals().getActiveViewLocation().;
                //UI.getCurrent().getPage().reload();
                
            });
        
        });

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.

https://vaadin.com/learn/training/v14-i18n

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.

    language
      .getSubMenu()
      .addItem(
        getTranslation("common.language.spanish"),
        e -> SessionUtils.changeSessionLanguage(new Locale("es", "US")));
    language
      .getSubMenu()
      .addItem(
        getTranslation("common.language.english"),
        e -> SessionUtils.changeSessionLanguage(new Locale("en", "US")))
      .setId("btnEnglish");
  }```
and
public static void changeSessionLanguage(Locale locale) {

    UI.getCurrent().getSession().setLocale(locale);

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

    // Set the cookie path.
    myCookie.setPath(VaadinService.getCurrentRequest().getContextPath());

    // Save cookie
    VaadinService.getCurrentResponse().addCookie(myCookie);

    UI.getCurrent().getPage().reload();

}```

It works great as long as you use getTranslation("some.text.key.in.your.properties.file") wherever you display text.