1

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?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Hanynowsky
  • 2,970
  • 5
  • 32
  • 43

2 Answers2

3

You need to wrap your master template in a

<f:view locale="#{LanguageBean.localeCode}">

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalusC thanks for the quick response! I reverted to the solution from the given link you mentioned and the problem remains! It is crazy! – Hanynowsky May 24 '11 at 13:53
  • Did you use **exactly the same** code? Even though it's just for testing purposes. You might have made some minor changes or omitted some important parts. – BalusC May 24 '11 at 14:42
  • @BalusC I applied the exact example given! I made no changes! It might be a problem of refreshing! Maybe my template is not well designed as I am using Primefaces – Hanynowsky May 24 '11 at 15:54
  • That's not an exact example then. My example didn't use anything of ` – BalusC May 24 '11 at 15:56
  • @BalusC ok I see what you mean! I am going to apply the example on another plain web app for testing and give the results here! Thank you BalusC – Hanynowsky May 24 '11 at 16:36
  • @BalusC I have just tested on a simple Web app without Faces libraries. And it is working fine! The language lives through the whole session. But in concerned web app, it is still like it is a request behavior not a session's! As you said, there might be some ajax calls or events fired and causing the problem! And I guess it is PrimeFaces ! Next step, I think is to user the on my test app, and see if the problem is reproduced! But I recall having it working fine the first time I used ! Anyway! If you have any clues, they will be so helpful! – Hanynowsky Jun 06 '11 at 11:56
  • 1
    @BalusC @maple_shaft EDIT: I removed the : ` fr ` from `faces-config.xml` and now everything is working fine for the Locale persistence. Isn't that weird? – Hanynowsky Jun 06 '11 at 12:46
2

The problem is that when you are setting your Locale in the valueChangeListener you are setting it on the ViewRoot which lives and dies with the current view, and not the session.

You are storing your LocaleCode in the SessionScoped managed bean so you can set the Locale in the ViewRoot for every page and this should fix your problem.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
  • Thanks for the prompt reply! You mean I need to add on my 321 pages? :( – – Hanynowsky May 24 '11 at 14:00
  • Hahaha!! Thats a big web application ^_^! I am sure that you can figure out some automated way to do this. – maple_shaft May 24 '11 at 14:10
  • Yeah indeed it is a big one {\/ô-ô\/} ! Well I tested with a simple web app with no third libraries included! Same Locale managed bean and same jsf code. and It works fine on all page. There was no need to nest the on all pages! As it was already present on the template! Well It really had not to :) I think there is some issue caused by the of PrimeFaces and it is hard to spot! – Hanynowsky Jun 06 '11 at 11:45