You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Julien Martin <ba...@gmail.com> on 2008/07/10 07:31:59 UTC

PhaseListener: before or after which phase should I set the locale?

Hello,
I use restfaces an I have UrLs such as mydomain.com/fr/book/etc
where /fr/ is the desired locale. I have tried retrieving the locale string
from the URL and setting the locale from a PhaseListener before the
renderResponse phase but it does not work i.e. when I change the locale in
the address bar to /en/book/etc it will not change the locale. Any clue?
Thanks in advance,
JUlien.

Here is the phase listener:

code:
------------------------------


public class DebugPhaseListener implements PhaseListener {

private static transient Logger log =
Logger.getLogger("com.jeanbaptistemartin.util");

public DebugPhaseListener() {
log.debug("DebugPhaseListener constructor");
}

public void afterPhase(PhaseEvent phaseEvent) {
log.debug("after---> " + phaseEvent.getPhaseId());
}

public void beforePhase(PhaseEvent phaseEvent) {
Locale locale =
extractLocale(FacesContext.getCurrentInstance().getExternalContext().getRequestServletPath());
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}

public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}

private Locale extractLocale(String servletPath) {
String[] array = servletPath.split("/");
return new Locale(array[0]);

}
}

------------------------------



Here is the managed bean (I actually use spring):

code:
------------------------------


@Component("sculptureView")
@Scope("request")
public class SculptureView {

private Logger log = Logger.getLogger("com.jeanbaptistemartin.view");
private JbmService service;
private Sculpture sculpture;

public SculptureView() {
log.info("Sculpture()");
}

@Autowired
public SculptureView(JbmService service) {
log.info("Sculpture(JbmService service)");
this.service = service;
}

@Instance("#{sculptureView}")
@HttpAction(value = "sculptureAction", pattern = "{locale}/sculpture/{id}")
public String getBookById(@Param("id") int id, @Param("locale")String locale) {
this.sculpture = service.findByID(id);
log.debug("Locale: "+ locale);
return "/sculpture.jsp";
}

public Sculpture getSculpture() {
return sculpture;
}

public void setSculpture(Sculpture sculpture) {
this.sculpture = sculpture;
}

}

------------------------------



The problem I get is that the jsp page still display today's date in
french...
Julien.

Re: PhaseListener: before or after which phase should I set the locale?

Posted by Simon Lessard <si...@gmail.com>.
Hi,

Definitely AFTER PhaseId.RESTORE_VIEW, we used that with success in many
projects in the past here.


Regards,

~ Simon

On Thu, Jul 10, 2008 at 1:17 PM, Zigc Junk <zi...@gmail.com> wrote:

> Try restore view phase which is the first phase of the life cycle.
>
> regard
>
> Bill
>
> On Thu, Jul 10, 2008 at 12:31 AM, Julien Martin <ba...@gmail.com> wrote:
> > Hello,
> > I use restfaces an I have UrLs such as mydomain.com/fr/book/etc
> > where /fr/ is the desired locale. I have tried retrieving the locale
> string
> > from the URL and setting the locale from a PhaseListener before the
> > renderResponse phase but it does not work i.e. when I change the locale
> in
> > the address bar to /en/book/etc it will not change the locale. Any clue?
> > Thanks in advance,
> > JUlien.
> >
> > Here is the phase listener:
> >
> > code:
> > ________________________________
> >
> > public class DebugPhaseListener implements PhaseListener {
> >
> >
> > private static transient Logger log =
> > Logger.getLogger("com.jeanbaptistemartin.util");
> >
> > public DebugPhaseListener() {
> > log.debug("DebugPhaseListener constructor");
> > }
> >
> > public void afterPhase(PhaseEvent phaseEvent) {
> >
> > log.debug("after---> " + phaseEvent.getPhaseId());
> > }
> >
> > public void beforePhase(PhaseEvent phaseEvent) {
> > Locale locale =
> >
> extractLocale(FacesContext.getCurrentInstance().getExternalContext().getRequestServletPath());
> >
> > FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
> > }
> >
> > public PhaseId getPhaseId() {
> > return PhaseId.RENDER_RESPONSE;
> > }
> >
> > private Locale extractLocale(String servletPath) {
> > String[] array = servletPath.split("/");
> >
> > return new Locale(array[0]);
> >
> > }
> > }
> >
> >
> > ________________________________
> >
> > Here is the managed bean (I actually use spring):
> >
> > code:
> > ________________________________
> >
> > @Component("sculptureView")
> > @Scope("request")
> > public class SculptureView {
> >
> > private Logger log = Logger.getLogger("com.jeanbaptistemartin.view");
> > private JbmService service;
> >
> > private Sculpture sculpture;
> >
> > public SculptureView() {
> > log.info("Sculpture()");
> > }
> >
> > @Autowired
> > public SculptureView(JbmService service) {
> > log.info("Sculpture(JbmService service)");
> >
> > this.service = service;
> > }
> >
> > @Instance("#{sculptureView}")
> > @HttpAction(value = "sculptureAction", pattern =
> "{locale}/sculpture/{id}")
> > public String getBookById(@Param("id") int id, @Param("locale")String
> > locale) {
> >
> > this.sculpture = service.findByID(id);
> > log.debug("Locale: "+ locale);
> > return "/sculpture.jsp";
> > }
> >
> > public Sculpture getSculpture() {
> > return sculpture;
> > }
> >
> > public void setSculpture(Sculpture sculpture) {
> >
> > this.sculpture = sculpture;
> > }
> >
> > }
> >
> > ________________________________
> >
> > The problem I get is that the jsp page still display today's date in
> > french...
> > Julien.
>

Re: PhaseListener: before or after which phase should I set the locale?

Posted by Zigc Junk <zi...@gmail.com>.
Try restore view phase which is the first phase of the life cycle.

regard

Bill

On Thu, Jul 10, 2008 at 12:31 AM, Julien Martin <ba...@gmail.com> wrote:
> Hello,
> I use restfaces an I have UrLs such as mydomain.com/fr/book/etc
> where /fr/ is the desired locale. I have tried retrieving the locale string
> from the URL and setting the locale from a PhaseListener before the
> renderResponse phase but it does not work i.e. when I change the locale in
> the address bar to /en/book/etc it will not change the locale. Any clue?
> Thanks in advance,
> JUlien.
>
> Here is the phase listener:
>
> code:
> ________________________________
>
> public class DebugPhaseListener implements PhaseListener {
>
>
> private static transient Logger log =
> Logger.getLogger("com.jeanbaptistemartin.util");
>
> public DebugPhaseListener() {
> log.debug("DebugPhaseListener constructor");
> }
>
> public void afterPhase(PhaseEvent phaseEvent) {
>
> log.debug("after---> " + phaseEvent.getPhaseId());
> }
>
> public void beforePhase(PhaseEvent phaseEvent) {
> Locale locale =
> extractLocale(FacesContext.getCurrentInstance().getExternalContext().getRequestServletPath());
>
> FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
> }
>
> public PhaseId getPhaseId() {
> return PhaseId.RENDER_RESPONSE;
> }
>
> private Locale extractLocale(String servletPath) {
> String[] array = servletPath.split("/");
>
> return new Locale(array[0]);
>
> }
> }
>
>
> ________________________________
>
> Here is the managed bean (I actually use spring):
>
> code:
> ________________________________
>
> @Component("sculptureView")
> @Scope("request")
> public class SculptureView {
>
> private Logger log = Logger.getLogger("com.jeanbaptistemartin.view");
> private JbmService service;
>
> private Sculpture sculpture;
>
> public SculptureView() {
> log.info("Sculpture()");
> }
>
> @Autowired
> public SculptureView(JbmService service) {
> log.info("Sculpture(JbmService service)");
>
> this.service = service;
> }
>
> @Instance("#{sculptureView}")
> @HttpAction(value = "sculptureAction", pattern = "{locale}/sculpture/{id}")
> public String getBookById(@Param("id") int id, @Param("locale")String
> locale) {
>
> this.sculpture = service.findByID(id);
> log.debug("Locale: "+ locale);
> return "/sculpture.jsp";
> }
>
> public Sculpture getSculpture() {
> return sculpture;
> }
>
> public void setSculpture(Sculpture sculpture) {
>
> this.sculpture = sculpture;
> }
>
> }
>
> ________________________________
>
> The problem I get is that the jsp page still display today's date in
> french...
> Julien.

RE: PhaseListener: before or after which phase should I set the locale?

Posted by Guy Bashan <gu...@gmail.com>.
Hi,

I used a while ago the same technique and it seems to be working fine.

Try using: <s:loadBundle>.

 

Guy.

 

From: Julien Martin [mailto:balteo@gmail.com] 
Sent: Thursday, July 10, 2008 8:32 AM
To: MyFaces Discussion
Subject: PhaseListener: before or after which phase should I set the locale?

 

Hello,
I use restfaces an I have UrLs such as mydomain.com/fr/book/etc
where /fr/ is the desired locale. I have tried retrieving the locale string
from the URL and setting the locale from a PhaseListener before the
renderResponse phase but it does not work i.e. when I change the locale in
the address bar to /en/book/etc it will not change the locale. Any clue?
Thanks in advance,
JUlien.

Here is the phase listener:

code:

  _____  




public class DebugPhaseListener implements PhaseListener {






 


private static transient Logger log =
Logger.getLogger("com.jeanbaptistemartin.util");


 


public DebugPhaseListener() {


log.debug("DebugPhaseListener constructor");


}


 


public void afterPhase(PhaseEvent phaseEvent) {






log.debug("after---> " + phaseEvent.getPhaseId());


}


 


public void beforePhase(PhaseEvent phaseEvent) {


Locale locale =
extractLocale(FacesContext.getCurrentInstance().getExternalContext().getRequ
estServletPath());






FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);


}


 


public PhaseId getPhaseId() {


return PhaseId.RENDER_RESPONSE;


}


 


private Locale extractLocale(String servletPath) {


String[] array = servletPath.split("/");






return new Locale(array[0]);


 


}


}


 
  _____  




Here is the managed bean (I actually use spring):

code:

  _____  








@Component("sculptureView")


@Scope("request")


public class SculptureView {


 


private Logger log = Logger.getLogger("com.jeanbaptistemartin.view");


private JbmService service;






private Sculpture sculpture;


 


public SculptureView() {


log.info("Sculpture()");


}


 


@Autowired


public SculptureView(JbmService service) {


log.info("Sculpture(JbmService service)");






this.service = service;


}


 


@Instance("#{sculptureView}")


@HttpAction(value = "sculptureAction", pattern = "{locale}/sculpture/{id}")


public String getBookById(@Param("id") int id, @Param("locale")String
locale) {






this.sculpture = service.findByID(id);


log.debug("Locale: "+ locale);


return "/sculpture.jsp";


}


 


public Sculpture getSculpture() {


return sculpture;


}


 


public void setSculpture(Sculpture sculpture) {






this.sculpture = sculpture;


}


 


}
  _____  




The problem I get is that the jsp page still display today's date in
french...
Julien.