首先!不要評論我使用MessageSource作爲服務的原因。因爲我正處於學習OSGi和Spring的階段。JSP使用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"/>
和在模塊的context.xml豆:
<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"/>
它不會改變!但在同一時間,我通過使用控制器某些翻譯爲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頁面中不是。
有人知道我失蹤了嗎?或者如何解決它?
感謝您閱讀,直到這裏,併爲長期的問題抱歉。
模塊的context.xml:
<bean id="localeChangeInterceptor" scope="bundle"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
OSGi的context.xml中: