File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes JSF and Locale Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF and Locale" Watch "JSF and Locale" New topic

JSF and Locale

Hernán Quevedo
Ranch Hand

Joined: Mar 18, 2009
Posts: 31

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:

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

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

Joined: Jun 25, 2001
Posts: 17410

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.

An IDE is no substitute for an Intelligent Developer.
Martin Lopez Ochoteco

Joined: May 28, 2009
Posts: 15

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

Joined: Mar 18, 2009
Posts: 31

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

Joined: May 28, 2009
Posts: 15
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:



I have these files to match:

I hope it helps.
I agree. Here's the link:
subject: JSF and Locale
It's not a secret anymore!