webtier@glassfish.java.net

Re: Changing Locale Dynamically

From: <webtier_at_javadesktop.org>
Date: Mon, 07 Jun 2010 13:12:08 PDT

Well, thanks to some advice from folks here (thanks nash_era!), here is what I finally did:

Problem:
- Calling setLocale on the view
   *** Triggered when you reload the page
   *** [b]Not [/b]triggered when you redisplay the page after running action listener (or submitting form)
- Setting the Locale of the UIViewRoot
   *** Triggered when you redisplay the page after running action listener (or submitting form)
   *** [b]Not [/b]triggered when you reload the page (Because the Locale is reset to the default)

Solution
- Do both!

So, in the ActionListener I did this:
  public void swapLocale1(ActionEvent event) {
    switchLocale();
  }
  
  private void switchLocale() {
    isEnglish = !isEnglish;
    Locale newLocale;
    if (isEnglish) {
      newLocale = ENGLISH;
    } else {
      newLocale = SPANISH;
    }
    FacesContext.getCurrentInstance().getViewRoot().setLocale(newLocale);
  }

Then, in the facelets page, I did this:
<f:view locale="#{formSettings.locale}">
I also added this to the FormSettings class:
  public Locale getLocale() {
    if (isEnglish) {
      return(ENGLISH);
    } else {
      return(SPANISH);
    }
  }

As I said before, doing either one (setting the UIViewRoot or using f:view with the locale attribute) was not enough. The above works, but it feels like a kludgy hack to me. I strongly suspect that I am misunderstanding the JSF lifecycle, and if I understood it properly, I would have a simpler approach.

Any suggestions? BTW, I put a ZIP of the above project in http://coreservlets.com/temp/ in case anyone wants to try it out or look at my code.

Thanks!

          - Marty
------
JSF 2.0 Training: http://courses.coreservlets.com/public-courses/jsf2/
[Message sent by forum member 'martyhall']

http://forums.java.net/jive/thread.jspa?messageID=473212