javaspringinternationalizationosgieclipse-virgo

JSP Spring internationalization using OSGi as service changing locale not working properly


First of all! Don't judge me the reason that I'm using MessageSource as a service. Since I'm in a phase learning OSGi and Spring.

I have a project that has many modules, in their pages, since I'm making internationalization for it. I saw that they use the same messages, so I put the codes in a common module that every module uses it. And I shared the message as a service osgi-context.xml:

<osgi:service ref="messageSource" interface="org.springframework.context.support.ReloadableResourceBundleMessageSource"/>
<osgi:service ref="localeResolver" interface="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<osgi:service ref="localeChangeInterceptor" interface="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>

and in module-context.xml the beans:

<bean id="messageSource" scope="bundle" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="localeResolver" scope="bundle"
      class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="et" />
</bean>

<bean id="localeChangeInterceptor" scope="bundle"
      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
</bean>

in the module that uses the service:

<osgi:reference id="messageSource" interface="org.springframework.context.support.ReloadableResourceBundleMessageSource"/>
<osgi:reference id="localeResolver" interface="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<osgi:reference id="localeChangeInterceptor" interface="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>

So internationalization works! But not completely... the problem comes when I try to change the locale, It partially works. The jsp pages where I use tag message like:

<spring:message code="general.welcome"/>

It does not change! But in the same time I pass some translations using a Controller to a JavaScript var like:

//Some page.jsp

<script>
    translations = ${translations == null? '{}' : translations};
</script>

Since the controllers are wired to the messageSource:

@Autowired
MessageSource messageSource;
...
//the way that the request is returned by a method
//A map in JSON using messageSource is return 
model.addAttribute("translations", someJSONmap);

It's working!

So in the controller the locale change is working, but in the JSP pages it isn't.

Do anyone know what I am missing? Or how to fix it?

Thanks for reading until here and sorry for the long question.


Solution

  • The problem was solved by removing the service:

    module-context.xml:

    <bean id="localeChangeInterceptor" scope="bundle"
          class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>
    

    osgi-context.xml:

    <osgi:service ref="localeChangeInterceptor" interface="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
    

    and put it into the module, which is using the service, applicationContext.xml:

    <mvc:interceptors>
        ...
        <bean id="localeChangeInterceptor"
          class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="lang" />
        </bean>
    </mvc:interceptors>