webtier@glassfish.java.net

Re: Changing Locale Dynamically

From: <webtier_at_javadesktop.org>
Date: Mon, 31 May 2010 20:25:59 PDT

hi
Saw your example

I was able to solve your problem in 2 steps
1) changed swapLocale, added one line
public void swapLocale(ActionEvent event) {
        System.out.println("[swapLocale] Setting isEnglish to " + !isEnglish);
        isEnglish = !isEnglish;

       FacesContext.getCurrentInstance().getViewRoot().setLocale(isEnglish?ENGLISH:SPANISH);
    }

2)instead of f:loadBundle used following in faces-config
    <application>
        <resource-bundle>
            <base-name>messages</base-name>
            <var>msgs</var>
        </resource-bundle>
    </application>

it seems that <resource-bundle> is required.

The solution even works if you remove the f:view completely or even if you keep the f:loadBundle, but <resource-bundle> in faces-config is required to swap locales.
======
the getLocale() method call happens in start phase i.e RESTORE_VIEW as per the jsf 2.0 spec:
2.2.1 Restore View
Examine the FacesContext instance for the current request. If it already contains a UIViewRoot:
■ Set the locale on this UIViewRoot to the value returned by the getRequestLocale() method on the ExternalContext for this request.
=======
a better idea would be to remove f:view completely.Locale as such is set by FacesContext.getCurrentInstance().getViewRoot().setLocale() method.
<f:view> as such is longer mandatory in JSF 2.0. if you remove it, you shall see getLocale() call happening after swapLocale() in the RENDER_RESPONSE phase.
[Message sent by forum member 'nash_era']

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