This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSF and Locale

 
Hernán Quevedo
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I have a JSP app which redirects to a JSF app, and there is a http session attribute setting the language. I need to change the locale of the JSF according to this and not according to the browser, since in mozilla shows up in english and in IE in spanish.

I'm trying to do so by having the faces context change the locale:

System.out.println(FacesContext.getCurrentInstance().getViewRoot().getLocale());
UserInfoDTO userInfoDto = (UserInfoDTO) request.getSession( ).getAttribute("userInfo");
String logLang = userInfoDto.getLoginLanguage();
FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(logLang,""));
System.out.println(FacesContext.getCurrentInstance().getViewRoot().getLocale());

It changes the language because of the first and last system.out, but the labels don't seem affected by this.

Any ideas?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18092
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's generally considered rude to override the user's preferences on locale and language as he/she indicated in the browser settings.

But if you must, changing the FacesContext isn't a good place to do it. The FacesContext is created (and destroyed) on each HTTP request/response cycle, so any changes made to it will disappear very fast. You'd need to store your changes in a more durable place - probably a session object.

Beyond that, I can't say, since so far all my JSF apps have been monotonously mono-lingual.
 
Martin Lopez Ochoteco
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have to ask because you didn't specify. Are you sure your faces-config file is well configured? Do you have your resource files (*.properties) for the language you are setting?
Please confirm this so we can discard it. You can test if it's working by showing in your JSF an object of type date or Timestamp, it will change it's format depending on wich locale you have.
 
Hernán Quevedo
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey.

Yeah, the faces-config is in order: locale-config and all. The bundle package has messages and messages_en properties files.
Currently, internet explorer is showing up in spanish, and firefox in english.

The thing is that users can choose the language when logging into the system, and so, the labels should change accordingly.

These preferences are captured by one servlet which sets up an attribute encapsulating user info like username, module, security profile, etc., and language of preference. I tried to change it in the first page redirected by the servlet but is not working.

Maybe if I have the servlet set up the header in respect to the browser language... I don't know.
 
Martin Lopez Ochoteco
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
edit: also look at the locale constructor, you can invoke it only with the language information. No need to pass "" as Country.

Look, the code is OK. All I can guess is a matter of configuration. Look at my code:

My bean:



My JSF:



Config:



I have these files to match:
mensajesG037.properties
mensajesG037_es.properties
mensajesG037_en.properties
mensajesG037_de.properties

I hope it helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic