Switching languages

I am using UI.getCurrent().setLocale(targetLocale); to switch from english to german in my main page. When I now navigate from the welcome page to another page, its in German. But the inital welcome page doesnt change, it stays english. When I try reload, all pages are english and switching to german doesnt work. Any ideas?

My reload was UI.getCurrent().getPage().reload(); but that didnt help as mentioned abouve


TL:DR: don’t use setLocale yourself, instead use an I18nProvider and implement LocaleChangeObserver where needed

i have that provider

ah ok the observer part hmm

i only have a “SImpleI18nprovider”…

out of an example. ok will check the observer thanks :slightly_smiling_face:

I have to correct myself, you still need to call setLocale (see e.g. https://github.com/vaadin/flow/blob/fdf626cf1a1a0f8e3cae6c398eac05b207e29a7c/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/LocaleChangeView.java#L65) but it’s recommended to react with the observer on the change to re-translate your components (just set labels etc. in there and you are good to go)

well i am already having property files with de and en locales. and already using this:

Button loginButton = new Button(
simpleI18NProvider.getTranslation(SimpleI18NConstants.WELCOME_BUTTON_LOGIN, getLocale()));

in example

will have to check this but it seems all my locale stuff now needs to be in this event?

that would need me to change a lot of code?

All attached components receive those events, so technically I would implement it on my Views and Forms and in there the translation can take place without much change (but I don’t know your code base)

simple views with horizontal layouts and text fields and stuff nothing too fancy :wink:

ok will play with it :slightly_smiling_face:

And if you get lazy, just create a “LocaleAwareLayout extends Hori… impl Observer” where you can probably put most of the redundant code and just iterate over the childrens and check / re-translate them

well… all my views already had a private void init() method annotated with @PostConstruct. these methods were building the views. inside init() there was stuff like addButtons(), addElementxy…()

i now made another method, moved the logic there and called this method inside init() AND inside the observer event