7 Replies Latest reply on Dec 26, 2006 2:56 AM by Manuel Aznar Pérez

    Change Locale sequence

    Manuel Aznar Pérez Newbie

      When change language, in an action in the same page catch old language, that have it before, with method "Locale.instance().getLanguage()"

      I change language with localeSelector :

       <h:form id="selectLanguage">
       <h:selectOneMenu value="#{localeSelector.language}">
       <f:selectItems value="#{localeSelector.supportedLocales}"/>
       </h:selectOneMenu>
       <h:commandButton action="#{localeSelector.select}" value="#{messages['ChangeLanguage']}"/>
       </h:form>
      


      After in action I have :

       String language = Locale.instance().getLanguage();
      


      When change language the action is executed too.

      I explain :


      View : Default "es" -> selectOneMenu = "español", properties = messages_es
      Action : Locale.instance().getLanguage() = "es"


      View: Change "en" -> selectOneMenu = "English", properties = messages_en
      Action: Locale.instance().getLanguage() = "es"


      View: Change "es" -> selectOneMenu = "español", properties = messages_es
      Action: Locale.instance().getLanguage() = "en"


      View: Change "en" -> selectOneMenu = "English", properties = messages_en
      Action: Locale.instance().getLanguage() = "es"
      .
      .
      .
      .

      How change language before enter in my action?

      Or any other idea?

      Thanks.

        • 1. Re: Change Locale sequence
          Pete Muir Master

          Huh? I don't really understand, can you show the code where you call the action and the whole of the action.

          • 2. Re: Change Locale sequence
            Manuel Aznar Pérez Newbie

            -Page:

            <!DOCTYPE composition 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"
             xmlns:s="http://jboss.com/products/seam/taglib"
             xmlns:a="https://ajax4jsf.dev.java.net/ajax"
             xmlns:ice="http://www.icesoft.com/icefaces/component">
            <head>
             <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
             <title>Reservas</title>
             <link href="stylesheet/theme.css" rel="stylesheet" type="text/css" />
             <link href="stylesheet/#{inicio.asociacion.hojaEstilos}" rel="stylesheet" type="text/css" />
             <script language='javascript' src='js/calendario/popcalendar.js'></script>
            </head>
            
            <body>
            
             <table border="0">
             <tr>
             <td align="center"><h:graphicImage id="banner" url="img/banners/#{inicio.asociacion.banner}" /></td>
             </tr>
             <tr>
             <td class="texto">
             <h:outputText value="#{messages['inicio.page.desc1']}" escape="false"/>
             </td>
             </tr>
             <tr class="headerBarClara" align="center">
             <td class = "textoBlanco" align="center">
             <h:outputText value="#{messages['inicio.page.titulo']}" escape="false"/>
             <h:form id="selectLanguage">
             <h:selectOneMenu value="#{localeSelector.language}">
             <f:selectItems value="#{localeSelector.supportedLocales}"/>
             </h:selectOneMenu>
             <h:commandButton action="#{localeSelector.select}" value="#{messages['ChangeLanguage']}"/>
             </h:form>
             </td>
             </tr>
             <tr>
             <td align="center" class="texto">
             <h:messages globalOnly="true" styleClass="message"/>
            
             <h:form id="inicio">
             <div>
             <s:validateAll>
             <div>
             <s:decorate>
             <h:outputLabel for="diaLlegada">#{messages['inicio.page.param.diaLlegada']}</h:outputLabel>
             <h:inputText id="diaLlegada" value="#{inicio.diaLlegada}" required="true">
             <s:convertDateTime pattern="dd/MM/yyyy"/>
             <a:support event="onblur" reRender="checkinDateErrors"/>
             </h:inputText>
             <img src="img/dtpick.gif" style="margin-left:5px" onClick="javascript:popUpCalendar(this, document.forms['inicio'].elements['inicio:diaLlegada'], 'dd/mm/yyyy')"/>
             <br/>
             <a:outputPanel id="checkinDateErrors"><s:message/></a:outputPanel>
            <!--
             <s:decorate>
             <ice:selectInputDate id="diaLlegada2" value="#{inicio.diaLlegada}" renderAsPopup="true" partialSubmit="true"/>
             <br/>
             <s:message/>
             </s:decorate>
            -->
             </s:decorate>
             </div>
            
             <div>
             <s:decorate>
             <h:outputLabel for="diaSalida">#{messages['inicio.page.param.diaSalida']}</h:outputLabel>
             <h:inputText id="diaSalida" value="#{inicio.diaSalida}" required="true">
             <s:convertDateTime pattern="dd/MM/yyyy"/>
             <a:support event="onblur" reRender="checkoutDateErrors"/>
             </h:inputText>
             <img src="img/dtpick.gif" style="margin-left:5px" onClick="javascript:popUpCalendar(this, document.forms['inicio'].elements['inicio:diaSalida'], 'dd/mm/yyyy')"/>
             <br/>
             <a:outputPanel id="checkoutDateErrors"><s:message/></a:outputPanel>
             </s:decorate>
             </div>
            
             </s:validateAll>
             </div>
             <div class="actionButtons">
             <h:commandButton id="inicio" value="#{messages['inicio.page.buscar.button']}"
             action="#{inicio.inicio}"/>
             <h:commandButton id="buscarAvanzado" value="#{messages['inicio.page.buscarAvanzado.button']}"
             action="#{inicio.listado}"/>
             <h:commandButton id="borrar" value="#{messages['inicio.page.borrar.button']}"
             action="#{inicio.inicio}"/>
             <s:link id="listado" value="#{messages['inicio.page.buscarAvanzado.button']}"
             action="#{inicio.listado}"/>
             </div>
             </h:form>
            
             </td>
             </tr>
             <tr class = "headerBarClara">
             <td height="16"></td>
             </tr>
             <tr>
             <td align="center" class="texto">
             <h:outputText value="#{inicio.asociacion.explicacion}" escape="false"/>
             </td>
             </tr>
             </table>
            
            </body>
            </html>
            
            


            -Action:

            
            package com.minalink.reservas;
            
            import java.util.ArrayList;
            import java.util.List;
            import javax.ejb.Remove;
            import javax.ejb.Stateful;
            import javax.persistence.EntityManager;
            import javax.persistence.PersistenceContext;
            import org.hibernate.validator.Length;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.annotations.In;
            import org.jboss.seam.annotations.Destroy;
            import org.jboss.seam.annotations.Logger;
            import org.jboss.seam.framework.EntityQuery;
            .
            .
            .
            import org.jboss.seam.log.Log;
            import org.jboss.seam.core.FacesMessages;
            import org.jboss.seam.core.Locale;
            import org.jboss.seam.core.LocaleSelector;
            
            @Stateful
            @Name("inicio")
            public class InicioBean implements Inicio {
            
             @Logger private Log log;
            
             @In
             FacesMessages facesMessages;
            
             @PersistenceContext
             private EntityManager em;
            
             AsociacionesLangHome asociacionesLangHome;
            
             private String diaLlegada;
            
             private String diaSalida;
            
             private int asociacionesId;
            
             private AsociacionesLang asociacion;
            
            
             //seam-gen method
             public String inicio() {
            
             log.info("inicio.inicio() action called with: #0 #1", diaLlegada, diaSalida);
             facesMessages.add( "inicio #0, #1", (Object) diaLlegada, (Object) diaSalida );
            
             return "success";
             }
            
             //add additional action methods
            
             @Length(max=10)
             public String getDiaLlegada() {
             return diaLlegada;
             }
            
             public void setDiaLlegada(String diaLlegada) {
             log.info("diaLlegada: #0 ", diaLlegada);
             this.diaLlegada = diaLlegada;
             }
            
             @Length(max=10)
             public String getDiaSalida() {
             return diaSalida;
             }
            
             public void setDiaSalida(String diaSalida) {
             log.info("diaSalida: #0 ", diaSalida);
             this.diaSalida = diaSalida;
             }
            
             public void setAsociacionesId(int asociacionesId) {
             this.asociacionesId = asociacionesId;
            
             String language = Locale.instance().getLanguage();
             System.out.println( "language : "+ language );
            
             asociacionesLangHome = new AsociacionesLangHome();
            
             AsociacionesLangId id = new AsociacionesLangId(asociacionesId, language);
             asociacionesLangHome.setAsociacionesLangId(id);
            
             try{
             this.setAsociacion(asociacionesLangHome.getInstance());
             }catch(Exception e){
             System.out.println( "asociacionesId: "+ asociacionesId );
             System.out.println( "language error: "+ language );
            
             LocaleSelector.instance().selectLanguage(ConfiguracionCte.DEFAULT_LANG);
             language = Locale.instance().getLanguage();
             System.out.println( "language 2 : "+ language );
            
             id = new AsociacionesLangId(asociacionesId, language);
             asociacionesLangHome.setAsociacionesLangId(id);
            
             this.setAsociacion(asociacionesLangHome.getInstance());
             }
             }
            
             public int getAsociacionesId() {
             return asociacionesId;
             }
            
             @Destroy @Remove
             public void destroy() {}
            
             public AsociacionesLang getAsociacion() {
             return asociacion;
             }
            
             public void setAsociacion(AsociacionesLang asociacion) {
             this.asociacion = asociacion;
             }
            
            }
            


            When I change language the page is reload --> method setAsociacionesId is called.
            But catch previous language value to change language.

            • 3. Re: Change Locale sequence
              Pete Muir Master

              Can you do this in #{inicio.inicio} (the action method) instead? By then the locale should be updated...

              • 4. Re: Change Locale sequence
                Manuel Aznar Pérez Newbie

                No.

                I enter in browser :

                http://localhost:8080/reservas/inicio.seam?asociacionesId=17

                When change language page is refresh.

                I don't call action "inicio" yet.

                • 5. Re: Change Locale sequence
                  Pete Muir Master

                  My only other is idea is to use immediate="true" on the locale selector.

                  • 6. Re: Change Locale sequence
                    Norman Richards Master

                    If you can simplify this example a bit, removing the irrelevant details, it would be easier for people to understand exactly what you are trying to do.

                    • 7. Re: Change Locale sequence
                      Manuel Aznar Pérez Newbie

                      It is simplified. There aren't irrelevant details.

                      If I change language with localeSelector, the locale change it after call action setParameters.

                      In this setParameters locale language is the previus language before I change with localeSelector.

                      If use immediate="true" in h:selectOneMenu --> Do same thing !!!
                      If use immediate="true" in h:commandButton --> Do nothing !!