I have form with user profile settings. One of these settings is the default language of the site. When user change the default language setting i want to change locale of current session. Method for handling the form:
@ManagedBean(name="userEditBean")
@RequestScoped
public class UserEditBean {
@ManagedProperty(value = LanguageBean.INJECTION_NAME)
private LanguageBean languageMB;
private AdminUsersEditForm editForm = new AdminUsersEditForm();
public String changeDefaultLanguage() {
editForm.getUser().setDefaultLanguage(editForm.getLocaleCode());
adminUserEditService.update(editForm.getUser());
languageMB.changeLocale(editForm.getLocaleCode());
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(languageMB.translate("user.edit.languageChanged")));
return "/pages/protected/user/edit.xhtml?faces-redirect=true";
}
And the changeLocale:
@ManagedBean(name="languageMB")
@SessionScoped
public class LanguageBean implements Serializable {
public void changeLocale(String newLocaleValue) {
//loop country map to compare the locale code
for (Map.Entry<String, Object> entry : countries.entrySet()) {
if(entry.getValue().toString().equals(newLocaleValue)){
FacesContext.getCurrentInstance()
.getViewRoot().setLocale((Locale)entry.getValue());
}
}
}
Language in DB is changed, but site remain in old locale. When i call changeLocale from eventListener for changing site language it works, so where is the problem. Thank you.
Update: I have done some debugging and the problem is in ViewHandlingStrategy#createView() on the line:
if (ctx.getViewRoot() != null) {
locale = ctx.getViewRoot().getLocale();
ctx.getViewRoot() apparently returns null and locale is not set grom getViewRoot(). But i have no idea, where my viewRoot "lost"