Vaadin14 LanguageSelect

Hi,

I jumpstarted a vaadin14 project from basic-starter-spring-gradle (exact version: 14.2.0 and I use the same for vaadin-bom). I followed the instructions and code samples from https://vaadin.com/directory/component/language-select. I setup the language images and implemented I18nProvider with all resource bundle files expected. The first time around, the page displays properly with the language select showing the correct images with the language names. However, the moment I select a language, nothing happens on the screen. I see a nul pointer stacktrace in the server logs (pasted below). After this event, all screens redirect to DefaultErrorHandler with obscure stacktrace (also caused by null pointer starting from BootStrap).

[qtp718571091-47]
 ERROR com.vaadin.flow.server.DefaultErrorHandler - 
java.lang.NullPointerException
        at com.vaadin.flow.component.UI.setLocale(UI.java:734)
        at com.vaadin.flow.server.VaadinSession.lambda$setLocale$0(VaadinSession.java:365)
        at java.base/java.util.HashMap$Values.forEach(HashMap.java:976)
        at java.base/java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1083)
        at com.vaadin.flow.server.VaadinSession.setLocale(VaadinSession.java:365)
        at ch.carnet.kasparscherrer.LanguageSelect.lambda$new$868b9db0$1(LanguageSelect.java:61)
        at com.vaadin.flow.component.internal.AbstractFieldSupport.lambda$addValueChangeListener$828eca10$1(AbstractFieldSupport.java:96)
        at com.vaadin.flow.component.ComponentEventBus.fireEventForListener(ComponentEventBus.java:205)
        at com.vaadin.flow.component.ComponentEventBus.fireEvent(ComponentEventBus.java:194)
        at com.vaadin.flow.component.Component.fireEvent(Component.java:359)
        at com.vaadin.flow.component.ComponentUtil.fireEvent(ComponentUtil.java:386)
        at com.vaadin.flow.component.internal.AbstractFieldSupport.setValue(AbstractFieldSupport.java:207)
        at com.vaadin.flow.component.internal.AbstractFieldSupport.setModelValue(AbstractFieldSupport.java:167)
        at com.vaadin.flow.component.AbstractField.setModelValue(AbstractField.java:225)
        at com.vaadin.flow.component.AbstractSinglePropertyField.handlePropertyChange(AbstractSinglePropertyField.java:352)
        at com.vaadin.flow.component.AbstractSinglePropertyField.access$200(AbstractSinglePropertyField.java:48)
        at com.vaadin.flow.component.AbstractSinglePropertyField$1.propertyChange(AbstractSinglePropertyField.java:325)
        at com.vaadin.flow.internal.nodefeature.ElementPropertyMap.lambda$fireEvent$2(ElementPropertyMap.java:452)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
        at com.vaadin.flow.internal.nodefeature.ElementPropertyMap.fireEvent(ElementPropertyMap.java:452)
        at com.vaadin.flow.internal.nodefeature.ElementPropertyMap.access$100(ElementPropertyMap.java:48)
        at com.vaadin.flow.internal.nodefeature.ElementPropertyMap$PutResult.run(ElementPropertyMap.java:163)
        at com.vaadin.flow.server.communication.ServerRpcHandler.runMapSyncTask(ServerRpcHandler.java:395)
        at com.vaadin.flow.server.communication.ServerRpcHandler.lambda$handleInvocations$0(ServerRpcHandler.java:389)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
        at com.vaadin.flow.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:389)
        at com.vaadin.flow.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:317)
        at com.vaadin.flow.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:89)
        at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
        at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1545)

I find this line of code to be the offender… if (!this.locale.equals(locale)) { in UI.class (line:734). This is invoked by getUIs().forEach(ui -> ui.setLocale(locale)); in VaadinSession.class (line:365). While I am still a newbie to completely wrap my head around the internal framework, I suspect the UI instances being iterated over do not have class level attribute locale populated. From the UI code (offending line), it seems that this value must be present for all UI instances.

I would like to know if there are structural prerequisites that I had missed, which could be potentially causing this issue? Or if the components are yet to keep pace with the core framework? If so, is there a workaround or alternate for this functionality? How do I get things working on Vadin 14?

Thanks.

Based on your two questions I assume that there is a problem with the locale.

Can you add System.out.println(Locale.getDefault()); in your application and post what is printed?

Hi Sathyanarayanan

I’m the author of that LanguageSelect add-on. The fact that it works upon first page load means that you did basically all of the setup for that component right already.
Based on your stacktrace, your UI seems to have no locale set in the first place, just like you suspected yourself. I believe this is done automatically by the returned value of I18NProvider::getProvidedLocales. Can you check if your implementation of that method returns the list of Locales that you have translations for?

Check out [Vaadin I18N localization]
(https://vaadin.com/docs/v14/flow/advanced/tutorial-i18n-localization.html#locale-selection-for-new-session) for more info