In my WebApp I created a managed Bean that allows me to change the Locale from French to english and vice versa using Event Change Listener.
package beans;
import java.util.Locale;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
@ManagedBean
@SessionScoped
public class LocaleBean {
private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
public Locale getLocale() {
return locale;
}
public String getLanguage() {
return locale.getLanguage();
}
public void setLanguage(String language) {
locale = new Locale(language);
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
}
and in my template.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="#{localeBean.language}"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:f="http://java.sun.com/jsf/core">
<f:view contentType="text/html" locale="#{localeBean.locale}" id="mescoca">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><ui:insert name="title"></ui:insert></title>
<h:outputStylesheet name="css/jsfcrud.css"/>
<!--<f:loadBundle var="bundle" basename="/Bundle"/> -->
</h:head>
<h:body style="font-size: small; font-family: Ubuntu,verdana;">
<h:form>
<p:panel closable="false" style="float: right;height: 50px;font-size: smaller" >
<h:panelGrid columns="2" style="text-align: center">
<h:outputText value="#{bundle.Language}"/>
<h:selectOneMenu value="#{localeBean.language}" onchange="submit()">
<f:selectItem itemValue="fr" itemLabel="Français" />
<f:selectItem itemValue="en" itemLabel="English" />
<f:selectItem itemValue="fr_FR" itemLabel="France"/>
<f:selectItem itemValue="en_US" itemLabel="US" />
</h:selectOneMenu>
</h:panelGrid>
</p:panel>
The others pages:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="/template.xhtml">
<ui:define name="title">
<h:outputText value="#{bundle.EditHistoryTitle}"></h:outputText>
</ui:define>
<ui:define name="body" >
<h:panelGroup id="messagePanel" layout="block">
<h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
</h:panelGroup>
<h:form>.....
It is working, but the problem is that the language gets back to its first value once I navigate to another page. Otherwise, When I change the language, it only affects the current page and once I move to another page, the localization bundle gets its default value in faces-config.xml
What I need is to make the language persistent through the whole session. Does anyone have a clue plz?