【问题标题】:JSP Spring internationalization using OSGi as service changing locale not working properly使用 OSGi 作为服务更改语言环境的 JSP Spring 国际化无法正常工作
【发布时间】:2013-08-16 09:12:42
【问题描述】:

首先!不要评判我使用 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 中的 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>

在使用服务的模块中:

<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 var,例如:

//一些page.jsp

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

由于控制器连接到消息源:

@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 页面中它不是。

有人知道我错过了什么吗?或者如何解决?

感谢您一直阅读到这里,很抱歉这个问题太长了。

【问题讨论】:

    标签: java spring internationalization osgi eclipse-virgo


    【解决方案1】:

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

    模块上下文.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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 2013-05-24
      相关资源
      最近更新 更多