0

I'd like to set the specific user locale when he login, in my JSF app.

To do this I get the user locale from his properties and set user locale via

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

during login fase on successful login.

My problem is that when I show messages to the user in subsequent fases during the user session the locale I get via

FacesContext context = FacesContext.getCurrentInstance();
    if (context != null && context.getViewRoot() != null) {
        locale = context.getViewRoot().getLocale();
        ... 

is always the default locale.

I've no other points in my code where I set locale, so I can't figure out why this happens.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
user1805879
  • 61
  • 1
  • 7
  • Possible duplicate: http://stackoverflow.com/q/4830588/870122 – perissf Jan 16 '13 at 10:53
  • It is because ViewRoot is request-scoped, so it will change with each new HTTP request. You'll need to create some session-scoped bean and store Locale there. – Paweł Dyda Jan 16 '13 at 11:32

2 Answers2

1

You're basically only setting the locale for the current view, the login page. You need to remember the user locale in the session scope and set it in all subsequent views in the same session as well.

You can do that by making userLocale a property of some session scoped bean (perhaps just the logged-in user itself?) and referencing it as <f:view locale> in your master template.

<f:view locale="#{user.locale}">

This will implicitly perform viewRoot.setLocale(user.getLocale()) for every single view.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

You should say something more in what why do you collect from request information about user preffered local but lets say that you get it from header accept-language

Then, as JSF specification says:

The UIViewRoot’s Locale is determined and set by the ViewHandler during the execution of the ViewHandler’s createView() method. This method must cause the active Locale to be determined by looking at the user’s preferences combined with the application’s stated supported locales.

So one thing is to set Locale in ViewRoot and to collect Locale from request header and another thing is to proper set supported locales in your config file.

In other part of specification you can read (see ViewHandler calculateLocale method):

Method Application.getSupportedLocales() defines what locales this JSF application is capable of supporting. This method should match such sources of data up and return the Locale object that is the best choice for rendering the current application to the current user.

Why is it going in such way? Because you cannot send response to user with locale preffered by user if your apps isn't supported to send respond with that locale. Now all you have to do (I hope so) is to configure your faces-config.xml in WEB-INF directory like this (also copied from JSF Specification):

<faces-config>
  <application>
    <locale-config>
      <default-locale>en</default-locale>
      <supported-locale>de</supported-locale>
      <supported-locale>fr</supported-locale>
      <supported-locale>es</supported-locale>
//    ...
    </locale-config>
  </application>
</faces-config>
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Michał Kupisiński
  • 3,765
  • 1
  • 19
  • 23