You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by gi...@mac.com on 2005/07/11 11:16:55 UTC

Maintain locale across views

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

Re: Maintain locale across views

Posted by Craig McClanahan <cr...@gmail.com>.
On 7/11/05, giannidoe@mac.com <gi...@mac.com> wrote:
> I tried with the RI and was still getting the same issue then I
> realised it may be something to do with the fact I was using
> <redirect/> in the navigation rule.
> If I take out the <redirect/> then the locale is maintained across
> views, with redirect it's not.
> 

That would definitely matter.  When you use redirects, each request
looks like a "new" JSF request, so there is no old state to restore
(and therefore no knowledge of what the previously selected locale
was).

> Thanks
> Gianni
> 

Craig

> 
> On 11/lug/05, at 18:28, Craig McClanahan wrote:
> 
> > On 7/11/05, Daniel Zwink <zw...@oio.de> wrote:
> >
> >> Hi,
> >>
> >>
> >>> 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?
> >>>
> >>
> >> AFAIK yes.
> >>
> >>
> >
> > Actually, that is *not* correct, for requests after the initial one
> > (where the browser's accept-language header is used to set the default
> > if the app doesn't do it).
> >
> > Details are in the JSF spec, section 2.2.1, but the basic idea is
> > that, if you are already in a flow of views managed by JSF, *and* if a
> > locale has been established, it should also be carried forward to any
> > created view -- the app should not have to do this manually on every
> > view.  The RI does it this way, by the way.
> >
> > Craig
> >
> 
>

Re: Maintain locale across views

Posted by gi...@mac.com.
I tried with the RI and was still getting the same issue then I  
realised it may be something to do with the fact I was using  
<redirect/> in the navigation rule.
If I take out the <redirect/> then the locale is maintained across  
views, with redirect it's not.

Thanks
Gianni


On 11/lug/05, at 18:28, Craig McClanahan wrote:

> On 7/11/05, Daniel Zwink <zw...@oio.de> wrote:
>
>> Hi,
>>
>>
>>> 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?
>>>
>>
>> AFAIK yes.
>>
>>
>
> Actually, that is *not* correct, for requests after the initial one
> (where the browser's accept-language header is used to set the default
> if the app doesn't do it).
>
> Details are in the JSF spec, section 2.2.1, but the basic idea is
> that, if you are already in a flow of views managed by JSF, *and* if a
> locale has been established, it should also be carried forward to any
> created view -- the app should not have to do this manually on every
> view.  The RI does it this way, by the way.
>
> Craig
>


Re: Maintain locale across views

Posted by Craig McClanahan <cr...@gmail.com>.
On 7/11/05, Daniel Zwink <zw...@oio.de> wrote:
> Hi,
> 
> > 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?
> 
> AFAIK yes.
> 

Actually, that is *not* correct, for requests after the initial one
(where the browser's accept-language header is used to set the default
if the app doesn't do it).

Details are in the JSF spec, section 2.2.1, but the basic idea is
that, if you are already in a flow of views managed by JSF, *and* if a
locale has been established, it should also be carried forward to any
created view -- the app should not have to do this manually on every
view.  The RI does it this way, by the way.

Craig

Re: Maintain locale across views

Posted by Daniel Zwink <zw...@oio.de>.
Hi,

> 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?

AFAIK yes.

> Do I need to set the locale myself on a per-view basis or should JSF  
> maintain the locale between views?

AFAIK yes, too :-).

> 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?

First question: Right!
You can store it in some (session scoped) bean with other user 
preferences for example.

> I also have tiles in the mix using MyFaces JspTilesViewHandlerImpl,  
> does this have any implications for how I need to handle  
> internationalization?

Don't know much about tiles.


Daniel
-- 
Orientation in Objects GmbH
Weinheimerstr. 68
D-68309 Mannheim
http://www.oio.de
Tel +49(0)621-71839-0
Fax. +49(0)621-71839-50

Re: Maintain locale across views

Posted by "ir.ing.Jan Dockx" <ja...@mac.com>.
We use a PhaseListener. Locale switching stores the locale the JSTL 
way, and then on each request this code is executed:

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">*/
   //------------------------------------------------------------------
   /** {@value} */
   public static final String CVS_REVISION = "$Revision: 1.3 $"; 
//$NON-NLS-1$
   /** {@value} */
   public static final String CVS_DATE = "$Date: 2005/07/08 18:55:14 $"; 
//$NON-NLS-1$
   /** {@value} */
   public static final String CVS_STATE = "$State: Exp $"; //$NON-NLS-1$
   /** {@value} */
   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>{@value}</strong></p>
    */
   public static final String FMT_LOCALE = 
"javax.servlet.jsp.jstl.fmt.locale";

   /**
    * Set the {@link 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
> <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.
>
> giannidoe@mac.com 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
>>
>>
>>
>
>
Met vriendelijke groeten,

Jan Dockx

PeopleWare NV - Head Office
Cdt.Weynsstraat 85
B-2660 Hoboken
Tel: +32 3 448.33.38
Fax: +32 3 448.32.66

PeopleWare NV - Branch Office Geel
Kleinhoefstraat 5
B-2440 Geel
Tel: +32 14 57.00.90
Fax: +32 14 58.13.25

http://www.peopleware.be/
http://www.mobileware.be/

Re: Maintain locale across views

Posted by Jozef Hribik <jo...@apsoft.sk>.
we set a locale on each page
 
<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.

giannidoe@mac.com 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
>
>
>