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";
}