4 Replies Latest reply on Sep 21, 2006 11:27 AM by redeagle

    How can I set the language for all portlets in the portal

    rkiesi

      Hi all,

      I try to write a portlet in which a user can select the language of the information displayed in all other portlets. So far I can change the language for each portlet seperately. Please see the code below for details.

      The managed Bean:

      public class ChangeLocaleBean {
      
      
       private java.lang.String bundleName;
      
       /**
       * Changes the language
       * @param event
       */
       public void changeLocale(ActionEvent event) {
       HtmlCommandLink cl = (HtmlCommandLink)event.getComponent();
       String localeString = (String)cl.getId();
       FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(localeString));
       }
      
       /**
       *
       * @return bundleName
       */
       public java.lang.String getBundleName() {
       return bundleName;
       }
      
       /**
       *
       * @param bundleName bundleName
       */
       public void setBundleName(java.lang.String bundleName) {
       this.bundleName = bundleName;
       }
      
      }


      The JSP:
      <%@ page contentType="text/html" isELIgnored="false"%>
      <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
      <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
      <f:loadBundle basename="#{changeLocaleBean.bundleName}" var="bundle" />
      
      <h:form>
       <h:panelGroup id="languageSelector">
       <h:commandLink id="de"
       actionListener="#{changeLocaleBean.changeLocale}">
       <h:graphicImage url="/images/at.gif" alt="#{bundle['language.german']}"
       title="#{bundle['language.german']}" />
       </h:commandLink>
      
       <h:commandLink id="en"
       actionListener="#{changeLocaleBean.changeLocale}">
       <h:graphicImage url="/images/gb.gif"
       alt="#{bundle['language.english']}"
       title="#{bundle['language.english']}" />
       </h:commandLink>
       </h:panelGroup>
      </h:form>


      part of the faces-config.xml
      <managed-bean>
       <managed-bean-name>changeLocaleBean</managed-bean-name>
       <managed-bean-class>
       at.join2learn.portlet.nodetypemanager.managedBeans.ChangeLocaleBean
       </managed-bean-class>
       <managed-bean-scope>application</managed-bean-scope>
       <managed-property>
       <property-name>bundleName</property-name>
       <property-class>java.lang.String</property-class>
       <value>at.join2learn.portlet.locales.bundle</value>
       </managed-property>
       </managed-bean>


      Thanks in Advance
      Reinhard

        • 1. Re: How can I set the language for all portlets in the porta
          antoine_h

          may be have a look at the CMSPortlet, to see how it manage with the locale.

          The locale is also defined in the user preferences (if logged in).

          if you want to manage the session locale (decided by the user, that ask for a different locale, even if not logged in), you may use a parameter stored in the session, at the Application scope (for all portlets to read it). you may search on the web, there are some articles that explain how to use parameters in the session.

          I don't use a parameter in the session, but a Inter Portlet Communication (IPC) that is based on messages exchanged between portlets.

          As the IPC store and provide the messages at a session level, it provide me with a storing this parameter (as a message) at the session level.
          that avoid to put many things in the session, that is not recommended for heavy load management.

          • 2. Re: How can I set the language for all portlets in the porta
            redeagle

            Hi @ all,

            i have a similiar problem like rkiesi.
            I want to change the localization of the whole portal without userlogin!
            I have build up a jsf portlet which contains a combo box where you can select your language and i store the selected language in the session:

            Code inside my managed bean:

            session.setAttribute("locale", selectedLanguage, PortletSession.APPLICATION_SCOPE);


            After that i wrote a new ServerInterceptor which will be called after the LocalInterceptor of the portal:
            public class LocaleInterceptor extends ServerInterceptor {
             private static Logger logger = LoggerFactory
             .getLogger(LocaleInterceptor.class);
            
             protected void invoke(ServerInvocation invocation) throws Exception,
             InvocationException {
            
             ServerRequest req = invocation.getRequest();
             HttpSession session = req.getContext()
             .getClientRequest().getSession(false);
             if(session!=null){
             String locale = (String) session.getAttribute("locale");
             logger.info("locale "+locale);
             logger.info("id: "+session.getId());
             }
             invocation.invokeNext();
             }
            
            }


            Everything is working fine. The ServerInterceptor is called but i only get a null- value out of the session.

            Both session that one i access in my jsf page and that one i access in my Interceptor have the same ids!

            Another point i have to mention is the project- structure:
            - the portlet where i want to change the locale is a web- project
            - the interceptor is in a separated project because the classloader needs the interceptor first.

            Anyone knows why i cannot get the value out of the session?
            (is it possible that i'm accessing this attribute)
            or is there another way to change the locale for the whole portlet?

            regards,
            Red

            • 3. Re: How can I set the language for all portlets in the porta
              halversp

               

              "Red Eagle" wrote:


              Another point i have to mention is the project- structure:
              - the portlet where i want to change the locale is a web- project
              - the interceptor is in a separated project because the classloader needs the interceptor first.

              Anyone knows why i cannot get the value out of the session?
              (is it possible that i'm accessing this attribute)
              or is there another way to change the locale for the whole portlet?

              regards,
              Red

              Probably because the portlet and the interceptor are accessing different sessions (your portlet.war's and the portal-core.war, respectively).

              p

              • 4. Re: How can I set the language for all portlets in the porta
                redeagle

                But why i have the same session ids?

                Red