【问题标题】:Internationalization (i18n) working, but accented characters aren't accented国际化 (i18n) 工作,但重音字符不重音
【发布时间】:2013-08-14 22:55:22
【问题描述】:

我有一个 Spring MVC J2EE 应用程序,它利用 Spring 的 localeChangeInterceptorCookieLocaleResolver 来呈现语言环境驱动的支持。这是可行的,但是当我尝试使用重音符号对字母进行编码时,前端无法按预期呈现它们。

这是我的 webmvc-config.xml 文件中的一个 sn-p:

    <!-- 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&amp;#243;n del Gerente de Distrito。我的不满是它在前端显示完全一样,而不是显示Aprobación del Gerente de Distrito

知道我哪里出错了吗?

【问题讨论】:

    标签: spring jsp internationalization locale spring-mongo


    【解决方案1】:

    .properties 文件的编码通常(除了少数例外)预期为 Latin-1。因此,为了呈现 Unicode 内容,您需要对 Latin-1 曲目之外的字符进行转义:

    district.manager.approval=Aprobaci\u00F3n del Gerente de Distrito

    或者使用可以编码为 UTF8 的 XML 属性文件。

    【讨论】:

      【解决方案2】:

      经过一番探索,我似乎遗漏了一个关键细节:这似乎只发生在我使用 JSTL &lt;c:set&gt; 以及编码无法正常工作的 Spring 标记时。事实证明,当使用&lt;c:out&gt; 时,您需要附带escapeXml="false" 属性。这是我所做的,它现在似乎工作正常:

      这是在一页中设置的

      <c:set var="headerScreenTitle">
          <spring:message code='district.manager.review.and.approval' />
      </c:set>   
      

      这是在导入的页面中使用的

      <c:out value="${headerScreenTitle}" escapeXml="false" />
      

      它给了我这个:

      REVISIÓN Y APROBACIÓN DEL GERENTE DE DISTRITO

      感谢大家的回复!

      【讨论】:

        【解决方案3】:

        如果您使用 html 实体(例如 &)并使用 &lt;spring:message code="property.name" /&gt; 标记打印值,请将“htmlEscape”属性设置为“false”:

        <spring:message code="property.name" htmlEscape="false" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-15
          • 2023-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-17
          相关资源
          最近更新 更多