我想知道什么/是否有一个等效于在表示层上完成的以下操作,以按区域设置在我的 message.properties 文件中检索外部化的 Spring 消息。这是它在 View 上的完成方式:
<spring:message code="ejournal.starttransaction"/>
我问这个是因为有一些 SimpleDateFormats 需要国际化,目前在我的表单 .java 文件中如下所示:
private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private SimpleDateFormat sdfDate=new SimpleDateFormat("yyyy-MM-dd");
private SimpleDateFormat sdfTime=new SimpleDateFormat("HH:mm:ss");
这些硬编码的日期格式很适合从 messages.properties 文件中检索,就像我的 View 调用国际化文本的方式一样。
如果这听起来像是一个非常幼稚或无知的问题,我很抱歉,但环顾四周似乎没有什么能与这个问题完全切合主题。如果有人对如何完成此操作有任何建议,请告诉我或建议一些初学者阅读。
如果您需要更多代码片段,请告诉我!
编辑-
消息源确实存在。现在尝试通过每个答案的注释来处理这个问题。
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>/WEB-INF/messages/message</value>
</list>
</property>
</bean>
编辑 2 -
Spring 版本是 2.5.4。
最佳答案
在Spring MVC中,你可以像下面这样配置一个ResourceBundleMessageSource
<bean name="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource" >
<property name="basename" value="ScreenMessages" />
</bean>
然后您将为每个区域设置一个文件。比如
- 美国英语 (en_US) - ScreenMessage_en_US.properties
- 中文-ScreenMessage_zh_CN.properties
- 默认 - ScreenMessage.properties
ResourceBundleMessageSource 是一个语言环境敏感的 bean,它将根据语言环境加载适当的文件。
在您的 Java 类中,您可以使用 bean 中的 @Value 注释检索日期格式,如下所示
@Value("#{ejournal.dateFormat}")
private String dateFormat;
@Value 注解仅从 spring 3 开始可用。如果您使用的是 Spring 2.5,则将 ResourceBundleMessageSource 注入(inject)到您的 bean 中
@Autowired
private ResourceBundleMessageSource messageSource;
现在您可以使用语言环境检索如下属性
Locale locale = LocaleContextHolder.getLocale();
String dateFormat = messageSource.getMessage('ejournal.dateFormat',null,locale);
关于java - 在 Form .java 文件中检索用于国际化的 Spring 外部化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23806549/