【问题标题】:In Java/Spring, how to gracefully handle missing translation values?在 Java/Spring 中,如何优雅地处理丢失的翻译值?
【发布时间】:2013-08-09 17:51:38
【问题描述】:

我正在为一个国际网站使用 Java + Spring。

两种语言是ZH和EN(中文和英文)。

我有 2 个文件:messages.properties(用于英文键/值对)和 messages_zh.properties。

该网站使用#springMessage 标签以英文编码。然后我使用 Poeditor.com 让翻译人员为每个英文短语提供中文值。所以,虽然英文的 messages.properties 文件总是完整的,虽然 messages_zh.properties 文件总是有所有的,但是messages_zh.properties 文件有时会有空白的 因为几天后我收到了翻译人员的翻译。

每当中文值缺失时,我需要我的系统显示等效的英文值(在我的网站上)。

当中文值不可用时,我如何告诉 Spring “回退”以使用英文?我需要在逐个值的基础上实现这一点。

现在,我在我的网站上看到缺少中文值的空白按钮标签。我宁愿在中文为空时使用英文(默认语言)。

【问题讨论】:

    标签: java spring spring-mvc localization translation


    【解决方案1】:

    您可以为此创建自己的自定义消息源。

    类似:

    public class SpecialMessageSource extends ReloadableResourceBundleMessageSource {
    
          @Override
          protected MessageFormat resolveCode(String code, Locale locale) {
             MessageFormat result = super.resolveCode(code, locale);
             if (result.getPattern().isEmpty() && locale == Locale.CHINESE) {
                return super.resolveCode(code, Locale.ENGLISH);
             }
             return result;
          }
    
          @Override
          protected String resolveCodeWithoutArguments(String code, Locale locale) {
             String result= super.resolveCodeWithoutArguments(code, locale);
             if ((result == null || result.isEmpty()) && locale == Locale.CHINESE) {
                return super.resolveCodeWithoutArguments(code, Locale.ENGLISH);
             }
             return result;
          }
       }
    

    并在spring xml中将此messageSource bean配置为

    <bean id="messageSource" class="SpecialMessageSource">
    .....
    </bean>
    

    现在要解决 Label,您将调用 MessageSource's 以下任一方法

    String getMessage(String code, Object[] args, Locale locale);
    String getMessage(String code, Object[] args, String defaultMessage, Locale locale);
    

    resolveCode() 将在您的消息标签具有参数并且您通过 args 参数传递这些参数时调用,如下所示
    invalid.number= {0} is Invalid
    然后你调用messageSource.getMessage("INVALID_NUMBER", new Object[]{2d}, locale)

    resolveCodeWithoutArguments() 将在您的消息标签没有参数并且您将 args 参数作为 null 传递时调用
    validation.success = Validation Success
    然后你调用messageSource.getMessage("INVALID_NUMBER", null, locale)

    【讨论】:

    • 我不确定谁对你投了反对票,但我会投你一票,并为你的答案表示赞赏,因为你让我走上了正确的道路。我不确定我的代码是否能满足我的所有需求。也许你能找到问题。
    • @Ryan 我更新了我的代码并根据您的需要添加了更多解释
    • (result.isEmpty() || result == null):也许 null 检查应该在 isEmpty 检查之前进行以避免 NPE :) ...无论如何 +1,这个答案对我有帮助
    【解决方案2】:

    @harrybvp 的回答让我走上了正轨。这是似乎对我有用的代码(并且当您模拟调用“super”的方法时可以进行单元测试):

        public class GracefulMessageSource extends org.springframework.context.support.ReloadableResourceBundleMessageSource {
    
            final static private Locale defaultLocale = Locale.ENGLISH;
    
            @Override protected MessageFormat resolveCode(final String code, final Locale locale) {
                MessageFormat result = superResolveCode(code, locale);
    
                if (result.toPattern().isEmpty() && !locale.equals(this.defaultLocale)) {
                    result = superResolveCode(code, this.defaultLocale);
                }
    
                return result;
            }
    
            @Override protected String resolveCodeWithoutArguments(final String code, final Locale locale) {
                String result = superResolveCodeWithoutArguments(code, locale);
    
                if (result.isEmpty() && !locale.equals(this.defaultLocale)) {
                    result = superResolveCodeWithoutArguments(code, this.defaultLocale);
                }
    
                return result;
            }
    
            protected MessageFormat superResolveCode(final String code, final Locale locale) {
                return super.resolveCode(code, locale);
    
            }
    
            protected String superResolveCodeWithoutArguments(final String code, final Locale locale) {
                return super.resolveCodeWithoutArguments(code, locale);
    
            }
    
        }
    

    在我的 webmvc-config.xml 中:

    <bean id="messageSource" class="com.mycode.fe.web.GracefulMessageSource">
        <property name="basename">
            <value>${content.path.config}/WEB-INF/messages</value>
        </property>
        <property name="defaultEncoding" value="UTF-8" />
        <property name="cacheSeconds" value="2"/>
        <property name="fallbackToSystemLocale" value="true"/>
    </bean>
    

    然后在我的 Velocity 视图模板中:

    对于没有参数的普通键:#springMessage('menu_item1')

    对于接受参数的键:#springMessageText('male_to_female_ratio' [3, 2])

    然后在messages.properties(英文翻译)中: male_to_female_ratio = There is a {0}:{1} male-to-female ratio.

    【讨论】:

      猜你喜欢
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 2011-02-02
      • 2019-06-18
      • 2023-03-21
      相关资源
      最近更新 更多