Announcement Announcement Module
Collapse
No announcement yet.
Internationalization: change language on web-page Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Internationalization: change language on web-page

    Hello,

    How to change language from the web-page (header)?

    Internationalization is implemented by the following principle:
    applicationContext.xml
    Code:
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="Bundle" /> </bean>
    Files containing messages:
    Bundle_en_US, Bundle_es_MX

    Each .jsp includes:
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    Each message:
    Code:
      <spring:message code="message.key.from.bundle"/>
    When i switch the locale language on browser level it does work indeed;
    But does NOT work, when i want to change the language from code:
    view plaincopy to clipboardprint?

    Code:
     Locale locale = Locale.getDefault();  
     System.out.println("Before setting, Locale is = " + locale);  
     // Setting default locale  
     locale = new Locale(localeToUse);  
     Locale.setDefault(locale);  
     System.out.println("After setting, Locale is = " + locale);  
    // Store it in session? How?
    How to implement change of the language from code for above approach?
    Precisely, i want to have a combo box on the header of the page and to be able to switch language -is their maybe a simpler approach than calling a Controller (servlet)


    Question also posted on:

    http://www.coderanch.com/t/507316/Sp...lization-combo


    Regards

  • #2
    I suggest reading the I18N chapter of spring and instead of hacking away on your own solution use the spring provided solution or extend that one...

    Comment


    • #3
      Thank you for your reply, Marten!

      Found
      http://static.springsource.org/sprin...rence/mvc.html

      I've extended dispatcher-servlet.xml

      Code:
      <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          <property name="interceptors">
              <list>
                  <ref bean="localeChangeInterceptor"/>
              </list>
          </property>

      Code:
        <bean id="localeChangeInterceptor"
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
          <property name="paramName" value="siteLanguage"/>
          </bean>
      
          <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
      Then in header.jsp i put
      Code:
                             <a href="index.htm?siteLanguage=en_US" >ENG</a>
                              &nbsp;
                              <a href="index.htm?siteLanguage=es">ESP</a>
                              &nbsp;

      Seems to work -let me know if you have any comments, please. I need to push this to production ASAP


      Thanks again!
      Regards

      Comment


      • #4
        Is there a way to rework language select to drop-down instead of links?


        Regards

        Comment


        • #5
          And why wouldn't there be... Simply create a drop down, the onselect calls the link including the selected item... Shouldn't be that hard...

          Comment

          Working...
          X