简体   繁体   中英

Spring boot MessageSource not working in application.properties

I am converting spring MVC to spring boot (2.0.3.RELEASE) project.

I am using this in spring boot, it works:

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:/locale/normal/message", "classpath:/locale/validation/message");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

I try to use spring boot auto-configuration (via application.properties ) as below (remove the manual bean configuration above):

spring.messages.basename=classpath:/locale/normal/message,classpath:/locale/validation/message

I tried below, it doesn't work too:

spring.messages.basename=/locale/normal/message,/locale/validation/message

I read their documentation below:

# 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.

I put the message properties file in the classpath:

src\main\resources\locale\normal
src\main\resources\locale\validation

I try a lot of combination, but it doesn't work, why?

The error I encounter :

javax.servlet.jsp.JspTagException: No message found under code 'login.timeout' for locale 'en'.

(I am pretty sure the message code is in my properties file for all locale.)

I am using Spring tag to load the localized message:

<spring:message code="${param.error}" />

尝试使用#代替$ - <spring:message code="#{param.error}" />

确保除了特定于语言环境的 messages.properties 之外,您还有默认的 messages.properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM