Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF Locale Change Listener not persistent
    primarykey
    data
    text
    <p>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.</p> <pre><code> 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); } } </code></pre> <p><strong>and in my template.xhtml:</strong></p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;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"&gt; &lt;f:view contentType="text/html" locale="#{localeBean.locale}" id="mescoca"&gt; &lt;h:head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;title&gt;&lt;ui:insert name="title"&gt;&lt;/ui:insert&gt;&lt;/title&gt; &lt;h:outputStylesheet name="css/jsfcrud.css"/&gt; &lt;!--&lt;f:loadBundle var="bundle" basename="/Bundle"/&gt; --&gt; &lt;/h:head&gt; &lt;h:body style="font-size: small; font-family: Ubuntu,verdana;"&gt; &lt;h:form&gt; &lt;p:panel closable="false" style="float: right;height: 50px;font-size: smaller" &gt; &lt;h:panelGrid columns="2" style="text-align: center"&gt; &lt;h:outputText value="#{bundle.Language}"/&gt; &lt;h:selectOneMenu value="#{localeBean.language}" onchange="submit()"&gt; &lt;f:selectItem itemValue="fr" itemLabel="Français" /&gt; &lt;f:selectItem itemValue="en" itemLabel="English" /&gt; &lt;f:selectItem itemValue="fr_FR" itemLabel="France"/&gt; &lt;f:selectItem itemValue="en_US" itemLabel="US" /&gt; &lt;/h:selectOneMenu&gt; &lt;/h:panelGrid&gt; &lt;/p:panel&gt; </code></pre> <p><strong>The others pages:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;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"&gt; &lt;ui:composition template="/template.xhtml"&gt; &lt;ui:define name="title"&gt; &lt;h:outputText value="#{bundle.EditHistoryTitle}"&gt;&lt;/h:outputText&gt; &lt;/ui:define&gt; &lt;ui:define name="body" &gt; &lt;h:panelGroup id="messagePanel" layout="block"&gt; &lt;h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/&gt; &lt;/h:panelGroup&gt; &lt;h:form&gt;..... </code></pre> <p>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 <strong>faces-config.xml</strong></p> <p>What I need is to make the language persistent through the whole session. Does anyone have a clue plz?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload