为什么 Thymeleaf International 仅适用于 ResourceBundleMessageSource [英] Why does Thymeleaf International only works with ResourceBundleMessageSource
问题描述
所以帮助回答这个问题 Thymeleaf + Spring (not Boot) - 如何显示来自 messageSource 的消息
So in helping answer This question Thymeleaf + Spring (not Boot) - how to show messages from messageSource
我发现如果您使用 ReloadableResourceBundleMessageSource
它将不起作用.Thymeleaf 会显示 ??app.name_pt_BR??而不是预期的结果.
I found that if you use the ReloadableResourceBundleMessageSource
it wouldn't working. Thymeleaf would display ??app.name_pt_BR?? instead of the expected result.
@Bean(name = "messageSource")
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/i18n/messages");
messageSource.setFallbackToSystemLocale(false);
messageSource.setCacheSeconds(0);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
当我切换到下面时,它按预期工作.
when I switch to the below it worked as expected.
@Bean(name = "messageSource")
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("/i18n/messages");
messageSource.setFallbackToSystemLocale(false);
messageSource.setCacheSeconds(0);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
为什么 ReloadableResourceBundleMessageSource 无法使用?
Why is the ReloadableResourceBundleMessageSource not able to be use?
为此问题添加代码:GitHub
推荐答案
找到答案!
根据 ReloadableResouceBundleMessageSource
的 javadocs
Per the javadocs for ReloadableResouceBundleMessageSource
Spring 特定的 MessageSource 实现,它使用指定的基本名称访问资源包,参与 Spring ApplicationContext 的资源加载.与基于 JDK 的 ResourceBundleMessageSource 相比,此类使用 Properties 实例作为消息的自定义数据结构,通过来自 Spring Resource 句柄的 PropertiesPersister 策略加载它们.此策略不仅能够根据时间戳更改重新加载文件,还能够加载具有特定字符编码的属性文件.它也会检测 XML 属性文件.
Spring-specific MessageSource implementation that accesses resource bundles using specified basenames, participating in the Spring ApplicationContext's resource loading. In contrast to the JDK-based ResourceBundleMessageSource, this class uses Properties instances as its custom data structure for messages, loading them via a PropertiesPersister strategy from Spring Resource handles. This strategy is not only capable of reloading files based on timestamp changes, but also of loading properties files with a specific character encoding. It will detect XML property files as well.
请注意,设置为basenames"属性的 basenames 的处理方式与 ResourceBundleMessageSource 的basenames"属性略有不同.它遵循不指定文件扩展名或语言代码的基本 ResourceBundle 规则,但可以引用任何 Spring 资源位置(而不是仅限于类路径资源).使用classpath:"前缀,仍然可以从类路径加载资源,但-1"(永久缓存)以外的cacheSeconds"值在这种情况下可能无法可靠地工作.
对于典型的 Web 应用程序,可以将消息文件放入 WEB-INF:例如一个WEB-INF/messages"基本名称可以使用WEB-INF/messages.properties"、WEB-INF/messages_en.properties"等安排以及WEB-INF/messages.xml"、WEB-INF"/messages_en.xml" 等.请注意,由于顺序查找,先前资源包中的消息定义将覆盖后面资源包中的消息定义.
For a typical web application, message files could be placed into WEB-INF: e.g. a "WEB-INF/messages" basename would fine a "WEB-INF/messages.properties", "WEB-INF/messages_en.properties" etc arrangement as well as "WEB-INF/messages.xml", "WEB-INF/messages_en.xml" etc. Note that message definitions in a previous resource bundle will override ones in a later bundle, due to sequential lookup.
此 MessageSource 可以轻松地在 ApplicationContext 之外使用:它将使用 DefaultResourceLoader 作为默认值,如果在上下文中运行,则只需使用 ApplicationContext 的资源加载器覆盖即可.它没有任何其他特定的依赖项.
This MessageSource can easily be used outside of an ApplicationContext: It will use a DefaultResourceLoader as default, simply getting overridden with the ApplicationContext's resource loader if running in a context. It does not have any other specific dependencies.
所以解决方法是提供路径.
so the fix is to provide the path.
来自
@Bean
public MessageSource messageSource()
{
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("messages");
messageSource.setFallbackToSystemLocale(false);
messageSource.setCacheSeconds(0);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
在 setBasename()
@Bean
public MessageSource messageSource()
{
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setFallbackToSystemLocale(false);
messageSource.setCacheSeconds(0);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
这篇关于为什么 Thymeleaf International 仅适用于 ResourceBundleMessageSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!