webtier@glassfish.java.net

Re: Changing Locale Dynamically

From: <webtier_at_javadesktop.org>
Date: Mon, 31 May 2010 05:21:53 PDT

> If we use swapLocale() like this, it works
>
> public void swapLocale(ActionEvent e) {
> if (locale == Locale.ENGLISH) {
> locale = new Locale("es");
> FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
> } else {
> locale = Locale.ENGLISH;
> FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
> }
> }
> (Now, we don't even need the locale attribute on
> f:view)

Actually, I should have mentioned in my original post that I had already tried this. I just went back and tried it again to be sure.

If I do the above but STILL set the attribute on f:view, I have the exact same behavior as before: when I press the button it calls getLocale BEFORE calling swapLocale, and I don't see the right properties until I manually reload the page.

If I do the above but WITHOUT setting the attribute on f:view, the properties never change at all.

> even i am curious about the lifecycle issue in your
> code. Hopefully somebody will put some light on it.

I also went back and carefully tested my original version as posted above, restarting the server and closing all browsers to be sure no session data was cached. Same behavior: pressing the button results in calling getLocale [b]first [/b]and swapLocale [b]second[/b].

I had assumed that I was just totally misunderstanding the lifecycle. I suppose it is also possible that there is a Mojarra bug. Just in case, I am running Mojarra 2.0.2 (FCS b10) on JDK 1.6.0.15 on Windows.

> Plus one more thing, jsf 2: complete reference says
> f:loadBundle is not recommended, use
> <resource-bundle>
> <base-name>com.all.Messages</base-name>
> <var>msgs</var>
> </resource-bundle>
>
> reason given is( from the complete reference book):
> Previous versions of JSF recommended using the
> f:loadBundle tag. Since JSF 1.2, this is no longer
> recommended because it fails in the case of Ajax and
> partial page updates because the f:loadBundle tag may
> not always be executed in those cases.

Hmm. Who am I to argue with Burns and Schalk? I have that book sitting right in front of me but hadn't read that part yet. This advice is news to me, so I will take it into account in the future. However, I am a little confused by the book example, since the advice you quote is at the top of page 286, yet the diagram at the bottom of page 287 still uses f:loadBundle. I am guessing the 287 diagram is just a leftover from the previous edition that they forgot to update.

Thanks for the tip!
[Message sent by forum member 'martyhall']

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