Localization

I see the docs say add “implements LocaleChangeObserver”, then i can set the texts public void localeChange()…

But the problem is that i already have a class with alot of texts like this, and i dont want to have all my components as globals to avoid clutter:

H1 header = new H1("Header");
Label label = new Label("Test");
add(header, label);
createPart1();
createPart2();
}

public void createPart1(){
Label label = new Label("Test1");
add(label);
}

public void createPart1(){
Label label = new Label("Test2");
add(label);
}

    @Override
    public void localeChange(LocaleChangeEvent localeChangeEvent) {
        //What do i do here? I dont want to have all my components as globals.
    }

YourClass extends Component implements LocaleObserver

new YourClass(“blub.property”)

would be my first choice

You can use getTranslation directly:Localization | Advanced Topics | Vaadin Docs

If a user wants to change the language then you would have to refresh your page.
But that’s usually not a big issue

FWIW we stopped using the LocaleChangeObserver pattern as it was complex for devs to understand the moment in time where this method is called during page load and its impact on the code (as you say, all components need to be instance vars)

Additionally, if your components require internationalized data (so not just the label of the combobox but also the options that the user can select are translated - this is not so uncommon), then onLocaleChange is a no go as well as it interferes with the binder. Basically our app became much simpler to understand once we removed it. One downside was that we need to refresh the page when the user changes language, but that’s not a big deal for us.