After the reload I can use .getLocale and and see that it works.
Get en, de, ru… depending on what is set in setLocale…
But how to reload the message texts…
When I change the locale on the login view, it works.
But not when I change it in a controller and then to the page reload…
This is the right way of changing locale, but the CurrentAuthentication still returns locale that was used in LoginView.
In general all XML loaders load messages with CurrentAuthentication#getLocale(), this is why nothing changes after page reloading. In this case, to change “login” locale you should reauthenticate user with new locale.
For instance:
@Autowired
private UserRepository userRepository;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private CurrentAuthentication currentAuthentication;
@Autowired
private Notifications notifications;
@Subscribe(id = "changeLocaleBtn", subject = "clickListener")
public void onChangeLocaleBtnClick(final ClickEvent<JmixButton> event) {
Authentication currAuth = currentAuthentication.getAuthentication();
// If user has logged in through LoginView
if (currAuth.getDetails() instanceof ClientDetails currDetails) {
// Set new locale
VaadinSession.getCurrent().setLocale(coreProperties.getAvailableLocales().g
// Copy client details from current authentication and change locale
ClientDetails newDetails = ClientDetails.builder().of(currDetails)
.locale(VaadinSession.getCurrent().getLocale())
.build();
// Use system token since we cannot get raw user's password.
// And set current authoritites
SystemAuthenticationToken authentication = new SystemAuthenticationToken(
currentAuthentication.getUser(),
currentAuthentication.getUser().getAuthorities());
authentication.setDetails(newDetails);
Authentication newAuth;
try {
newAuth = authenticationManager.authenticate(authentication);
} catch (AuthenticationException e) {
notifications.create("Error on changing locale")
.withType(Notifications.Type.ERROR)
.show();
return;
}
SecurityContextHelper.setAuthentication(newAuth);
UI.getCurrent().getPage().reload();
}
}
I don’t have any demo projects with SSO to test this, but you could try creating an AuthenticationLocaleResolver bean.
The CurrentAuthenticationImpl#getLocale() iterates over these beans until one of them returns a locale. For example, you could try to get the locale from the VaadinSession if it is enabled at that time.
Hi i tried this solution but seems not working, i need to change the locale to load the current data from the messages_xx.properties . Edit ok the problem is that i need to close the current tab page and re open it to visualize the changed labels, However the labels on the drawer (menu) are not reloaded