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

三世轮回 提交于 2019-12-12 02:09:29

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/18269811/jsp-spring-internationalization-using-osgi-as-service-changing-locale-not-workin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!