Spring XML消息源,bean XML中的getMessage? [英] Spring message source, getMessage in bean XML?
本文介绍了Spring XML消息源,bean XML中的getMessage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经设置了这样的资源:
I have set up my resources like this:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>locale\\messages</value>
</property>
</bean>
我的propertyFile:
My propertyFile:
battle.name=TestBattle
我想找到TestBattle文字当我使用bean时:
I would like to reach the text "TestBattle" when I use a bean:
<bean id="battlefield" class="com.mypackage.Battlefield" scope="prototype">
<constructor-arg index="0" value="battle.name" />
<constructor-arg index="1" ref="armies" />
</bean>
我想在此行中的propertyFile中重新发送消息
I want to refeer the message in the propertyFile in this line
<constructor-arg index="0" value="battle.name" />
有没有一种方法可以使用
Is there a way to do it without going into java using the
getMessage("battle.name",...
java中的代码?
推荐答案
至少,您可以使用spel来执行此操作。
At least, you could use spel to do it.
例如
<bean id="messageSourceAccessor" class="org.springframework.context.support.MessageSourceAccessor">
<constructor-arg ref="messageSource" />
</bean>
<bean id="battlefield" class="com.mypackage.Battlefield" scope="prototype">
<constructor-arg index="0" value="#{messageSourceAccessor.getMessage('battle.name')}" />
<constructor-arg index="1" ref="armies" />
</bean>
然而,如果您需要翻译许多代码,这似乎很麻烦。
However it seems cumbersome if you have to translate many codes.
其他选项使用字符串
到字符串
属性yEditor
进行翻译。
Other option is using a String
to String
PropertyEditor
to do the translation.
public class MessageSourcePropertyEditor extends PropertyEditorSupport {
private MessageSourceAccessor messageSourceAccessor;
public MessageSourcePropertyEditor(MessageSource messageSource) {
this.messageSourceAccessor = new MessageSourceAccessor(messageSource);
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
String value = text;
if (text.startsWith("i18n:")) {
value = messageSourceAccessor.getMessage(text.substring(5));
}
setValue(value);
}
}
public class MessageEditorRegistrar implements PropertyEditorRegistrar {
private MessageSource messageSource;
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(String.class, new MessageSourcePropertyEditor(messageSource));
}
public MessageSource getMessageSource() {
return messageSource;
}
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
}
并使用前缀 i18n:翻译代码,即
And use the prefix i18n: to translate codes, ie
<bean id="propertyEditorConfigure" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<bean class="message.MessageEditorRegistrar">
<property name="messageSource" ref="messageSource" />
</bean>
</list>
</property>
</bean>
<bean id="battlefield" class="com.mypackage.Battlefield" scope="prototype">
<constructor-arg index="0" value="i18n:battle.name" />
<constructor-arg index="1" ref="armies" />
</bean>
这篇关于Spring XML消息源,bean XML中的getMessage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文