title: Primefaces changing locale
body: <p>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:</p>
  
  <pre><code>@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";
      }
  </code></pre>
  
  <p>And the changeLocale:</p>
  
  <pre><code>@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&lt;String, Object&gt; entry : countries.entrySet()) {
                  if(entry.getValue().toString().equals(newLocaleValue)){
                      FacesContext.getCurrentInstance()
                              .getViewRoot().setLocale((Locale)entry.getValue());
                  }
              }
          }
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>Update:</strong>
  I have done some debugging and the problem is in ViewHandlingStrategy#createView() on the line:</p>
  
  <pre><code>if (ctx.getViewRoot() != null) {
      locale = ctx.getViewRoot().getLocale();
  </code></pre>
  
  <p>ctx.getViewRoot() apparently returns null and locale is not set grom getViewRoot().
  But i have no idea, where my viewRoot "lost"</p>