• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSF and Locale

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic