繁体   English   中英

使用OSGi作为服务更改语言环境的JSP Spring国际化无法正常工作

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

首先! 不要判断我将MessageSource用作服务的原因。 由于我处于学习OSGi和Spring的阶段。

因为我正在对其进行国际化,所以我的项目中的页面中包含许多模块。 我看到他们使用相同的消息,因此我将代码放在每个模块都使用的通用模块中。 我将消息共享为服务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"/>

并在module-context.xml中添加bean:

<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>

在使用该服务的模块中:

<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"/>

所以国际化的作品! 但不完全是...问题出在我尝试更改语言环境时,它部分起作用。 我在其中使用标记消息的jsp页面如下:

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

它不会改变! 但与此同时,我使用Controller将一些翻译传递给JavaScript变量,例如:

//一些page.jsp

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

由于控制器已连接到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);

工作正常!

因此,在控制器中,可以进行区域设置更改,但在JSP页面中却不能。

有人知道我在想什么吗? 或如何解决?

感谢您阅读到这里为止,并很抱歉提出这个问题。

通过删除服务解决了该问题:

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"/>

并将其放入使用服务applicationContext.xml的模块中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM