0

In our application we have a requirement to change the locale on the fly by clicking on button. I have referred the below link and was able to reload labels which are coming from the resource bundle with different locale.

Localization in JSF, how to remember selected locale per session instead of per request/view

But my problem is that we have to call SP to get the data in different language according to locale. So the data is not getting loaded from database when i am reloading my page. I have added my code below which i have written so far.

In XHTML Code:

<p:commandButton id="langbutton" value="#{language.lang}" 
                actionListener="#{language.langChanged}" 
                ajax="false" styleClass="loginlangbutton"/>

In Bean Class:

public String langChanged() {
        FacesContext context = FacesContext.getCurrentInstance();

        if (user != null && user.getLang().equalsIgnoreCase("E")) {

            user.setLang("F");

            lang = "English";

            FacesContext.getCurrentInstance().getViewRoot()
                    .setLocale(Locale.FRENCH);

            this.localeCode = Locale.FRENCH;

        } else {

            user.setLang("E");

            lang = "Français";

            FacesContext.getCurrentInstance().getViewRoot()
                    .setLocale(Locale.ENGLISH);

            this.localeCode = Locale.ENGLISH;
        }
        UserBean uBean = (UserBean) context.getApplication()
                .evaluateExpressionGet(context, "#{user}",
                        UserBean.class);
        uBean.setLoginDtl(user);

        return FacesContext.getCurrentInstance().getViewRoot().getViewId()
                + "?faces-redirect=true";

    } 
Community
  • 1
  • 1
NeJai
  • 1
  • 1
  • _"But my problem is that we have to call SP to get the data in different language according to locale. So the data is not getting loaded from database when i am reloading my page."_ And how can **we** help with that? (SP meaning 'service provider'?) Sounds like you have to 'just fix that' on your side... – Kukeltje May 18 '17 at 09:18
  • I have also referred some other answer by @BalusC but haven't found any solution yet. Any help or direction will be much appreciated – NeJai May 18 '17 at 09:19
  • SP means database Stored procedure – NeJai May 18 '17 at 09:19
  • But according to your question it is **impossible** to help... If you need to call an SP that does not give you new data, that is a very local totally not jsf related problem. – Kukeltje May 18 '17 at 09:20
  • I think this is JSF related problem because on locale change JSF is not recreating its view and showing the old values from previously created view and just changing the resource bundle values and not hitting the Database at all. – NeJai May 18 '17 at 09:22
  • If localization from resource bundles works, jsf has done its job. If you cannot get new labels from the database that is not jsf related. – Kukeltje May 18 '17 at 09:31
  • As i mentioned that label are getting loaded correctly only the data other than label is not getting reloaded according to locale from database because JSF is not triggering any DB hits. – NeJai May 18 '17 at 11:06
  • Triggering/hittig the database is not jsf's responsibility (if you developped correctly, jsf does not even know where the data comes from, database, ldap, static list, file). If data is e.g. lazy loaded by you, **you** have to trigger the refresh by e.g. setting something to null or whatever. So the problem you now encounter is not related to jsf nor is the code you posted – Kukeltje May 18 '17 at 11:10
  • Can anyone help me on this – NeJai May 21 '17 at 10:18

0 Answers0