在Java / Spring中,如何优雅地处理缺失的翻译值? [英] In Java/Spring, how to gracefully handle missing translation values?

查看:122
本文介绍了在Java / Spring中,如何优雅地处理缺失的翻译值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



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



每当中文值丢失时,我需要我的系统显示相当的英文值(在我的网站上)。 / p>

如果没有中文价值,我如何告诉Spring退回使用英语?我需要在价值的基础上实现这一点。



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

解决方案

你可以创建自己的自定义MessageSource用于此目的。



类似于:

 公共类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);
}
返回结果;
}

@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);
}
返回结果;
}
}

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

 < bean id =messageSourceclass =SpecialMessageSource> 
.....
< / bean>

现在要解决标签,你将调用 MessageSource的以下任一方法

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

resolveCode()将在您的时候调用消息标签有参数,你通过 args 参数传递这些参数如下所示

invalid.number = {0}无效

你调用 messageSource.getMessage(INVALID_NUMBER,new Object [] {2d},locale)



当您的消息标签没有参数并且传递 args <时,将调用resolveCodeWithoutArguments() / code>参数为null

validation.success =验证成功

并调用 messageSource.getMessage(INVALID_NUMBER,null,locale)


I'm using Java + Spring for an international website.

The 2 languages are ZH and EN (Chinese and English).

I have 2 files: messages.properties (for English key/value pairs) and messages_zh.properties.

The site is coded in English using #springMessage tags. I then use Poeditor.com to have translators provide Chinese values for each English phrase. So, although the English messages.properties file is always complete, and although the messages_zh.properties file always has all of the keys, the messages_zh.properties file will sometimes have blank values because I receive translations from the translators several days later.

I need my system to display the equivalent English value (on my website) whenever the Chinese value is missing.

How can I tell Spring to "fall back" to use English whenever a Chinese value isn't available? I need this to happen on a value-by-value basis.

Right now, I see blank button labels on my site wherever a Chinese value is missing. I'd rather that English (the default language) be used whenever the Chinese is blank.

解决方案

You could create your own Custom MessageSource for this purpose.

Something like:

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;
      }
   }

and configure this messageSource bean in spring xml as

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

Now to get resolved Label you will be invoking MessageSource's either of the below methods

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

resolveCode() will be called when your message label has arguments and you pass those arguments via args parameter like below
invalid.number= {0} is Invalid
and you invoke messageSource.getMessage("INVALID_NUMBER", new Object[]{2d}, locale)

resolveCodeWithoutArguments() will be called when your message label does not have arguments and you pass args parameter as null
validation.success = Validation Success
and you invoke messageSource.getMessage("INVALID_NUMBER", null, locale)

这篇关于在Java / Spring中,如何优雅地处理缺失的翻译值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆