[英]In Java/Spring, how to gracefully handle missing translation values?
我正在使用Java + Spring作为国际网站。
这两种语言是ZH和EN(中文和英文)。
我有2个文件:messages.properties(用于英文键/值对)和messages_zh.properties。
该网站使用#springMessage标签以英文编码。 然后我使用Poeditor.com让翻译人员为每个英语短语提供中文价值。 因此,虽然英文messages.properties文件总是完整的,虽然messages_zh.properties文件总是包含所有键 ,但messages_zh.properties文件有时会有空白值,因为几天后我会收到翻译人员的翻译。
每当中文值丢失时,我都需要我的系统显示相当的英文值(在我的网站上)。
如果没有中文价值,我如何告诉Spring“退回”使用英语? 我需要在价值的基础上实现这一点。
现在,我在我的网站上看到空白按钮标签,只要缺少中文值。 我宁愿在中文空白时使用英语(默认语言)。
您可以为此创建自己的自定义消息源。
就像是:
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);
当您的消息标签包含参数并且您通过args
参数传递这些参数时,将调用resolveCode()
如下所示
invalid.number= {0} is Invalid
并调用messageSource.getMessage("INVALID_NUMBER", new Object[]{2d}, locale)
当您的消息标签没有参数并且将args
参数传递为null时,将调用resolveCodeWithoutArguments()
validation.success = Validation Success
并调用messageSource.getMessage("INVALID_NUMBER", null, locale)
@ 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.