Java 在Spring MVC中使用cookie进行ICU定位
我需要使用ICU(Unicode的国际组件),因为我的应用程序应该支持JDK中不支持的区域设置,如“fa_IR” 我找到了一个使用Spring和Cookie提供此功能的配置Java 在Spring MVC中使用cookie进行ICU定位,java,spring,spring-mvc,localization,icu,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
<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中使用类似的配置
如果此配置不可能,是否有其他方式(如更改控制器等)?op问题中的示例是
CookieLocalerResolver
的配置,允许您从cookie解析请求的区域设置(默认情况下从Accept Language
头解析)和LocaleChangeInterceptor
,允许您通过请求更改语言环境
ICU与此无关。ICU是一个允许您格式化和翻译消息的库。它不关心语言环境的变化,这是春天的事
我假设您希望使用ICU进行区域设置感知的消息格式化,以替代Java的Java.text.MessageFormat
类
Spring在其MessageSourceSupport
类中使用MessageFormat
,该类由AbstractMessageSource
扩展,后者也由ResourceBundleMessageSource
扩展
SpringTemplateEngine engine = new SpringTemplateEngine();
// ...
engine.setTemplateEngineMessageSource(myCustomMessageSource);
return engine;
要将ICU的com.ibm.ICU.text.MessageFormat
类合并到应用程序中,您必须实现自己的MessageSrouce
类,该类提供MessageFormat。然后配置模板引擎以使用它。下面的代码片段显示了带有自定义MessageSource
的ThymileAF配置
SpringTemplateEngine engine = new SpringTemplateEngine();
// ...
engine.setTemplateEngineMessageSource(myCustomMessageSource);
return engine;
至少我还没有找到更好的解决办法
也许您会对我的文章感兴趣实现了一个与ICU兼容的消息源,可以在您的示例中使用它