I am developing an open source application for conferences to display the conference agenda and social media posts on a big screen. The language for the UI views should be configurable and not use the browser locale. Actually I have problems to implement that because I don’t fully understand how the selection of the resource bundle works.
In the directory src/main/resources/vaadin-i18n
I placed two property files:
translations.properties
translations_de.properties
The first one contains all translations in English and should be the fallback, because it has no language code in the name. The second one contains all German translations.
When I start my application, I have the UI in German. That tells me that Vaadin has found the files and was able to read the translations from them (at least from the German one). Now I want to force the UI to be English. This is what I tried so far:
On the view (there is only one view) I added an BeforeEnterObserver
to set the locale:
@Override
public void beforeEnter(BeforeEnterEvent event) {
UI.getCurrent().setLocale(Locale.ENGLISH);
}
The UI was still in German. So I tried to use a VaadinServiceInitListener
instead:
@Override
public void serviceInit(ServiceInitEvent event) {
event.getSource().addUIInitListener(uiInitEvent -> {
uiInitEvent.getUI().setLocale(Locale.ENGLISH);
});
}
Same, the UI was still in German. My last approach was to implement an own I18NProvider
:
@SpringComponent
public class CustomI18NProvider implements I18NProvider {
private static final List<Locale> SUPPORTED_LOCALES = List.of(Locale.ENGLISH);
@Override
public List<Locale> getProvidedLocales() {
return SUPPORTED_LOCALES;
}
@Override
public String getTranslation(String key, Locale locale, Object... params) {
return ResourceBundle.getBundle("vaadin-i18n/translations", Locale.ENGLISH).getString(key);
}
}
The UI is still in German…
Can please someone help me to find a working solution and hopefully, to understand why and how this works correctly?
PS: You can find the whole project with all sources on GitHub.