我在spring boot中使用它,它可以工作:
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:/locale/normal/message", "classpath:/locale/validation/message");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
application.properties
)如下所示(删除上面的手动bean配置):
spring.messages.basename=classpath:/locale/normal/message,classpath:/locale/validation/message
spring.messages.basename=/locale/normal/message,/locale/validation/message
我读了他们的书
documentation
# INTERNATIONALIZATION (MessageSourceProperties)
spring.messages.always-use-message-format=false # Whether to always apply the MessageFormat rules, parsing even messages without arguments.
spring.messages.basename=messages # Comma-separated list of basenames (essentially a fully-qualified classpath location), each following the ResourceBundle convention with relaxed support for slash based locations.
spring.messages.cache-duration= # Loaded resource bundle files cache duration. When not set, bundles are cached forever. If a duration suffix is not specified, seconds will be used.
spring.messages.encoding=UTF-8 # Message bundles encoding.
spring.messages.fallback-to-system-locale=true # Whether to fall back to the system Locale if no files for a specific Locale have been found.
spring.messages.use-code-as-default-message=false # Whether to use the message code as the default message instead of throwing a "NoSuchMessageException". Recommended during development only.
我将消息属性文件放在类路径中:
src\main\resources\locale\normal
src\main\resources\locale\validation
javax.servlet.jsp.JspTagException: No message found under code 'login.timeout' for locale 'en'.
我正在使用Spring标记加载本地化消息:
<spring:message code="${param.error}" />