button.caption=Click me
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.i18n.I18NProvider;
import com.vaadin.flow.i18n.LocaleChangeEvent;
import com.vaadin.flow.i18n.LocaleChangeObserver;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.VaadinService;
import java.util.Locale;
import java.util.ResourceBundle;
@Route("button")
public class ButtonView extends VerticalLayout implements LocaleChangeObserver {
private Button button;
public ButtonView() {
I18NProvider i18nProvider = () -> VaadinService.getCurrent().getInstantiator().getTranslationService();
button = new Button(getTranslation("button.caption"));
button.addClickListener(event -> System.out.println("Button clicked!"));
add(button);
}
@Override
public void localeChange(LocaleChangeEvent event) {
button.setText(getTranslation("button.caption"));
}
private String getTranslation(String key) {
Locale locale = VaadinService.getCurrentRequest().getLocale();
ResourceBundle resourceBundle = ResourceBundle.getBundle("messages", locale, VaadinService.getCurrent().getInstantiator().getTranslationService().getClassLoader());
return resourceBundle.getString(key);
}
}