java - Spring boot MessageSource 在 application.properties 中不起作用

标签 java spring spring-mvc spring-boot internationalization

我正在将 spring MVC 转换为 spring boot (2.0.3.RELEASE) 项目。

我在 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;
}

我尝试使用 spring boot 自动配置(通过 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}" />

最佳答案

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

关于java - Spring boot MessageSource 在 application.properties 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51535717/

相关文章:

java - 以编程方式发送短信 - 设置过期时间

java - 分离的对象如何在 hibernate 中工作

java - 如何制作调用其他注解的注解

java - 在 DispatcherServlet 中未找到名称为 'dispatcher' 的 URI [/TestSpring/] 的 HTTP 请求的映射

java - Spring MVC 自定义异常映射不起作用

Java Socket 不向服务器发送消息

java - 在 Eclipse 插件中使用非 OSGi 包

java - 一副牌

Spring Security 和多部分请求

java - 使用 Spring 和 Intellij 时无法解析 MVC View