使用cookie在Spring-MVC中使用ICU进行本地化

时间:2015-07-06 06:51:20

标签: java spring spring-mvc localization icu

我需要使用ICU(Unicode的国际组件)因为我的应用程序应该支持JDK中不支持的语言环境,如“fa_IR”。

我找到了一个使用Spring和Cookie提供此功能的配置。

    <beans:bean id="localeResolver"
            class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <beans:property name="defaultLocale" value="en" />
    <beans:property name="cookieName" value="myAppLocaleCookie"/>
    <beans:property name="cookieMaxAge" value="3600"/>
</beans:bean>

<interceptors>
    <beans:bean
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <beans:property name="paramName" value="locale" />
    </beans:bean>
</interceptors>

有没有办法为ICU使用类似的配置?

如果这个配置不可能是其他任何方式(比如更换控制器等)?

1 个答案:

答案 0 :(得分:0)

运算问题的示例是Accept-Language的配置,它允许您从Cookie中解析请求的区域设置(默认情况下它已从LocaleChangeInterceptor标头解析)和{{1允许您通过请求更改语言环境。

ICU与此无关。 ICU是一个允许您格式化和翻译消息的库。它并不关心那些Spring的东西的区域变化。

我假设您希望将ICU用于区域设置感知消息格式化,以替代Java的java.text.MessageFormat类。

Spring在MessageFormat的{​​{1}}类中使用MessageSourceSupportAbstractMessageSource扩展了ResourceBundleMessageSource

要在您的应用程序中加入ICU的com.ibm.icu.text.MessageFormat类,您必须实现自己的MessageSrouce类,该类提供MessageFormats。然后配置模板引擎以使用它。以下代码段显示了使用自定义MessageSource的Thymeleaf配置。

SpringTemplateEngine engine = new SpringTemplateEngine();
// ...
engine.setTemplateEngineMessageSource(myCustomMessageSource);
return engine;

至少我还没有找到更好的解决方案。

也许您会对我的文章Spring Boot internationalisation with database stored messages and IBM ICU

感兴趣