【问题标题】:Localization with ICU in Spring-MVC using cookie使用 cookie 在 Spring-MVC 中使用 ICU 进行本地化
【发布时间】:2015-09-23 05:11:33
【问题描述】:

我需要使用 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 使用类似的配置?

如果此配置不可能,是否还有其他方式(比如更改控制器等)?

【问题讨论】:

    标签: java spring spring-mvc localization icu


    【解决方案1】:

    操作问题中的示例是 CookieLocaleResolver 的配置,它允许您从 cookie 解析请求的语言环境(默认情况下它从 Accept-Language 标头解析)和 LocaleChangeInterceptor 允许您通过请求更改语言环境。

    ICU 与此无关。 ICU 是允许您格式化和翻译消息的库。它不关心 Spring 的语言环境更改。

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

    Spring 在它的 MessageSourceSupport 类中使用 MessageFormat,该类由 AbstractMessageSource 扩展,该类也由 ResourceBundleMessageSource 扩展。

    要将 ICU 的 com.ibm.icu.text.MessageFormat 类合并到您的应用程序中,您必须实现自己的提供 MessageFormats 的 MessageSrouce 类。然后配置您的模板引擎以使用它。以下 sn-p 显示了带有自定义 MessageSource 的 Thymeleaf 配置。

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

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

    也许你会对我的文章感兴趣Spring Boot internationalisation with database stored messages and IBM ICU

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    相关资源
    最近更新 更多