1

I am retrieving the user's locale from the database. Whenever user is login then page should display on that perticular language. So for this i am using below code in the JSF

    <f:metadata>
       <f:event type="preRenderView" listener="#{language.preferredLocale}"/>
    </f:metadata>
    <ui:define name="content_pane">
    <f:view locale="#{language.localeCode}"/>
    </ui:define>

    //language bean code is
    public void preferredLocale(ComponentSystemEvent e){
        // code to retrieve locale for particular user              
    }

But the above code is not working to set the locale on the page onload. is there anyother way i can set the locale on the page load?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user2625662
  • 21
  • 1
  • 3

2 Answers2

1

This is indeed not the right way. It's too late then. You need to make sure that <f:view locale> is already set right.

There are several ways to achieve this anyway, depending on your current application design. The simplest one would probably be the following:

@ManagedBean
@RequestScoped
public void LoginBean {

    @ManagedProperty("#{localeBean}")
    private LocaleBean localeBean;

    public void submit() {
        User user = userService.find(username, password);

        if (user != null) {
            localeBean.setLanguage(user.getPreferences().getLanguage());
            // ...
        }

        // ...
    }

    // ...
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Can you please explain how to add language on the JSF page? – user2625662 Jul 27 '13 at 15:21
  • Set it as ``. See for another example also http://stackoverflow.com/questions/4830588/jsf-locale-is-set-per-request-not-for-session/4830669#4830669 – BalusC Jul 27 '13 at 15:50
0

use this

<f:view beforePhase=#{bean.viewBeforePhase} >
Kemal Duran
  • 1,478
  • 1
  • 13
  • 19