我有一个 Spring MVC J2EE 应用程序,它利用 Spring 的 localeChangeInterceptor
和 CookieLocaleResolver
来呈现区域设置驱动的支持。这是有效的,但是当我尝试对带有重音符号的字母进行编码时,前端无法按预期呈现它们。
这是我的 webmvc-config.xml 文件中的一个片段:
<!-- Internalization and localization support -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
除此之外,我还有一些 message_xx.properties 文件,这些文件包含我的标签以呈现内容。包括像这样的带有重音符号的标签:district.manager.approval=Aprobación del Gerente de Distrito
。我的不满是,这在前端显示完全像这样,而不是向我显示 Aprobación del Gerente de Distrito
。
知道我可能哪里出错了吗?
最佳答案
.properties 文件的编码通常(除了少数异常(exception))预期为 Latin-1。因此,为了呈现 Unicode 内容,您需要转义超出 Latin-1 轨道的字符:
district.manager.approval=Aprobaci\u00F3n del Gerente de Distrito
或者使用可以编码为 UTF8 的 XML 属性文件。
关于spring - 国际化 (i18n) 工作,但重音字符不重音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18243689/