Spring MVC Sping Boot MessageSource不工作在application.properties

xdyibdwo  于 2023-10-24  发布在  Spring
关注(0)|答案(3)|浏览(222)

我正在将Spring MVC转换为Sping 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}" />
krcsximq

krcsximq1#

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

snvhrwxg

snvhrwxg2#

确保你有默认的messages.properties除了区域设置特定的messages.properties

roqulrg3

roqulrg33#

spring.messages. baseline应该没有前导斜杠:

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

相关问题