为什么我需要Spring Boot`messages.properties`而不是`messages_en.properties`? [英] Why do I need Spring Boot `messages.properties` instead of just `messages_en.properties`?
问题描述
我正在遵循本指南,并说要命名默认消息文件作为messages.properties
.为什么我不能将其命名为messages_en.properties
并将默认语言环境设置为英语?
I'm following this guide and it says to name the default messages file as messages.properties
. Why can't I name it messages_en.properties
and set the default locale to English?
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.ENGLISH);
return slr;
}
这不起作用(每次重新命名message.properties
时都必须硬停止并重新启动服务器-动态重载无法获取更改).它将打印出带有诸如<spring:message code="oops" text="default"/>
之类的标签的default
文本,因为找不到messages_en.properties
.
This doesn't work (have to hard stop and restart the server each time message.properties
is renamed - dynamic reload fails to pick up the changes). It will print out default
text with a tag like <spring:message code="oops" text="default"/>
because it can't find messages_en.properties
.
实际上,当我将浏览器默认语言设置为法语并设置为messages_fr.properties
并重新启动服务器时,它也同样找不到法语的密钥.
In fact, when I set my browser default language to French and have messages_fr.properties
, and restart the server, it is also failing to find the keys for French as well.
不使用Thymeleaf.无需允许用户选择语言.
Not using Thymeleaf. No need to allow user-selectable language.
参考: https://docs. spring.io/spring-boot/docs/1.5.17.RELEASE/reference/htmlsingle/(世界上只有一个国际化"实例,并且仅与应用程序属性有关).
Reference: https://docs.spring.io/spring-boot/docs/1.5.17.RELEASE/reference/htmlsingle/ (Only one instance of the world 'internationalization', and only regarding application properties).
推荐答案
I fixed it by using AcceptHeaderLocaleResolver
instead.
@Bean
public LocaleResolver localeResolver() {
AcceptHeaderLocaleResolver ahlr = new AcceptHeaderLocaleResolver();
ahlr.setDefaultLocale(Locale.ENGLISH);
return ahlr;
}
我以为该指南说它将在会话,cookie或标头中搜索语言环境,但他们只是说不同的子类分别实现了这些功能.
I thought the guide said it will search for the locale in the session, cookie, or header, but they were only saying that different sub-classes implement those features separately.
LocaleResolver接口具有可根据会话,cookie,Accept-Language标头或固定值确定当前语言环境的实现.
The LocaleResolver interface has implementations that determine the current locale based on the session, cookies, the Accept-Language header, or a fixed value.
我通过设置此应用程序属性,至少用于调试:
I fixed the live reload problem by setting this application property, at least for debugging:
spring.messages.cache-seconds=1
我还设置了此属性,以免弄乱我的src/main/resources
文件夹,因此我可以将所有邮件文件移到locales/
子目录中.
I also set this property so as not to clutter up my src/main/resources
folder, so I could move all the message files into the locales/
sub-directory.
spring.messages.basename=locales/messages
这篇关于为什么我需要Spring Boot`messages.properties`而不是`messages_en.properties`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!