package be.peopleware.jsf_I;
import java.util.Locale;
import java.util.Map;
import javax.faces.FacesException;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import be.peopleware.jsf_II.RobustCurrent;
/**
* With every faces request, get the locale from the JSTL settings,
* and put it in the view root. This needs to be done immediately
* before the render response phase, so that the handling of the request parameters
* (e.g., conversion and validation) still uses the locale of the request with
* which the page in which the user entered values.
*
* @author Jan Dockx
* @author PeopleWare n.v.
*/
public final class SetLocalePhaseListener implements PhaseListener {
/*<section name="Meta Information">*/
//------------------------------------------------------------------
/** [EMAIL PROTECTED] */
public static final String CVS_REVISION = "$Revision: 1.3 $"; //$NON-NLS-1$
/** [EMAIL PROTECTED] */
public static final String CVS_DATE = "$Date: 2005/07/08 18:55:14 $"; //$NON-NLS-1$
/** [EMAIL PROTECTED] */
public static final String CVS_STATE = "$State: Exp $"; //$NON-NLS-1$
/** [EMAIL PROTECTED] */
public static final String CVS_TAG = "$Name: $"; //$NON-NLS-1$
/*</section>*/
private static final Log LOG = LogFactory.getLog(SetLocalePhaseListener.class);
/*<construction>*/
//------------------------------------------------------------------
// Default constructor is needed
/*</construction */
/**
* <p>Where JSTL stores it's locale setting. This is
* <code>javax.servlet.jsp.jstl.core.Config.FMT_LOCATE</code>.</p>
*
* <p><strong>[EMAIL PROTECTED]</strong></p>
*/
public static final String FMT_LOCALE = "javax.servlet.jsp.jstl.fmt.locale";
/**
* Set the [EMAIL PROTECTED] UIViewRoot#getLocale()}
*/
public void beforePhase(PhaseEvent event) throws FacesException {
LOG.debug("before RENDER_RESPONSE: setting locale");
FacesContext fc = event.getFacesContext(); // cannot be null
assert fc != null;
UIViewRoot vr = fc.getViewRoot(); // cannot be null
LOG.debug("UIViewRoot = " + vr.getViewId());
assert vr != null;
Map sessionMap = fc.getExternalContext().getSessionMap(); // cannot be null
assert sessionMap != null;
// get JSTL locale
Locale locale = (Locale)RobustCurrent.sessionMap().get(FMT_LOCALE);
LOG.debug("JSTL locale retrieved: " + locale);
if (locale != null) {
vr.setLocale(locale);
LOG.debug("locale of UIViewRoot set to " + locale);
}
}
public final void afterPhase(PhaseEvent event) {
// NOP
}
public final PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
}
Code is copyright PeopleWare n.v. http://www.peopleware.be/. Permission is granted to use this code under the Apache License v2.
On 11 Jul 2005, at 12:28, Jozef Hribik wrote:
we set a locale on each page<x-tad-smaller>Met vriendelijke groeten,
<f:view locale="#{loginMBean.user.locale}">
loginMBean has session-scope. Our web-app requires login, anonymous is not allowed. The first page (login page) has hardcoded german locale, but user can define his favouritr locale in preferences and after successfull login the loginMBean.user.locale is initialized.
[EMAIL PROTECTED] wrote:
I'm starting out with JSF and I'd appreciate some clarification regarding internationalization:
I'm using a select menu and backing bean to call setLocale() on the current view, this works fine and I can change the locale of the view in question. But when I navigate to another view the locale reverts back to the browser default.
Is this correct behavior?
Do I need to set the locale myself on a per-view basis or should JSF maintain the locale between views?
If this is the case then I guess I need to store the view's local in the session and use this to set the locale for subsequent views. Where and how would be the best way to do this in terms of the JSF lifecycle?
I also have tiles in the mix using MyFaces JspTilesViewHandlerImpl, does this have any implications for how I need to handle internationalization?
Thanks
Gianni
__________ Informacia od NOD32 1.1164 (20050708) __________
Tato sprava bola preverena antivirusovym systemom NOD32.
http://www.eset.sk
__________ Informacia od NOD32 1.1164 (20050708) __________
Tato sprava bola preverena antivirusovym systemom NOD32.
http://www.eset.sk
Jan Dockx
</x-tad-smaller><x-tad-smaller>
PeopleWare NV - Head Office</x-tad-smaller><x-tad-smaller>
Cdt.Weynsstraat 85
B-2660 Hoboken
Tel: +32 3 448.33.38
Fax: +32 3 448.32.66 </x-tad-smaller><x-tad-bigger>
</x-tad-bigger><x-tad-smaller>
PeopleWare NV - Branch Office Geel</x-tad-smaller><x-tad-smaller>
Kleinhoefstraat 5
B-2440 Geel
Tel: +32 14 57.00.90
Fax: +32 14 58.13.25</x-tad-smaller><x-tad-bigger>
</x-tad-bigger><x-tad-smaller>
http://www.peopleware.be/
</x-tad-smaller><x-tad-smaller>http://www.mobileware.be/</x-tad-smaller>
smime.p7s
Description: S/MIME cryptographic signature

