spring mvc i18n:如果密钥不在属性文件中,如何设置默认区域设置?

发布于 2025-01-08 14:38:27 字数 283 浏览 0 评论 0原文

假设如果我的其中一个属性文件中没有密钥,我会收到如下异常:

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'label.cancel' for locale 'en '。

假设如果它在我的 messages_ch_CN.properties 中不可用,是否有任何方法可以在该文件中不存在它时应该检查 messages_en_En 文件。 或者是否有人已经实施了任何解决办法。

suppose if i don't have a key in my one of the properties file i get a exception like :

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'label.cancel' for locale 'en'.

suppose if it is not available in my messages_ch_CN.properties is there any way that if it is not present in that file it should check in messages_en_En file.
or is there any work around any one has implemented.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

装纯掩盖桑 2025-01-15 14:38:27

例如,您有: 2 种语言

messages_ch_CN.properties  /*in the property file lang=Chinese*/
messages_en_EN.properties  /*in the property file lang=English*/
messages.properties  /*in the property file lang=English default*/

messages.properties 这是默认属性,它始终包含整个应用程序中使用的每个键。

NAME-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <context:component-scan base-package="com.example.controller"/>

    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/resources/"/>

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:com/example/i18n/messages"/>
        <property name="fallbackToSystemLocale" value="false"/>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

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

    <bean id="localeResolver"
          class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en"/>
    </bean>

</beans>

- 表示如果您没有属性 messages_ch_CN.propertiesmessages_en_EN.properties 或者在这些属性之一中没有必要的键,例如:title,spring将使用messages.properties 作为默认值

,如果 - spring 使用部署环境的用户区域设置,但它不好,因为用户的部署环境可能与我们提供的语言不同。如果用户的区域设置与我们提供的语言不同,spring 会使用 messages.properties

For example you have: 2 language

messages_ch_CN.properties  /*in the property file lang=Chinese*/
messages_en_EN.properties  /*in the property file lang=English*/
messages.properties  /*in the property file lang=English default*/

messages.properties this is default property, which always contains every key used throughout your application.

and NAME-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <context:component-scan base-package="com.example.controller"/>

    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/resources/"/>

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:com/example/i18n/messages"/>
        <property name="fallbackToSystemLocale" value="false"/>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

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

    <bean id="localeResolver"
          class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en"/>
    </bean>

</beans>

<property name="fallbackToSystemLocale" value="false"/> - says that, if you don't have property messages_ch_CN.properties or messages_en_EN.properties or in one of those property there is no necessary key, for example: title, spring will use messages.properties as default

if <property name="fallbackToSystemLocale" value="true"/> - spring uses user locale of deployment environment, but it not good because user's deployment environment, can different from the languages that we have provided. If user's locale different from the languages that we have provided, spring uses messages.properties.

随风而去 2025-01-15 14:38:27

一种解决方法是对您正在使用的 messageSource 进行子类化(即 ReloadableResourceBundleMessageSource)并覆盖它的 resolveCode(),这样:

  • 它会查找指定代码和区域设置的消息(通过调用 super.resolveCode(code, locale))。
  • 如果没有找到,则会查找具有默认区域设置的区域(通过调用 super.resolveCode(code, defaultLocale))。

然后使用新创建的类作为messageSource

One workaround would be to subclass the messageSource you're using (ie ReloadableResourceBundleMessageSource) and override it's resolveCode() so that:

  • it looks for a message for specified code and locale (by calling super.resolveCode(code, locale)).
  • if it doesn't find it, then it looks for one with default locale (by calling super.resolveCode(code, defaultLocale)).

Then use that newly created class as messageSource.

枫林﹌晚霞¤ 2025-01-15 14:38:27

始终提供默认的 messages 文件(没有 locale 规范),该文件始终包含整个应用程序中使用的每个键。如果不子类化其中一个 ResourceBundle 实现,就不可能自动查找所选语言环境之外的另一种语言环境

Always provide a default messages file (without locale specification), which always contains every key used throughout your application. It is not possible to look automatically for another locale than the chosen one without subclassing one of the ResourceBundleimplementations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文