当默认区域设置为“en”时,ReloadableResourceBundleMessageSource 无法找到消息
为什么 Spring“ReloadableResourceBundleMessageSource”在 Locale.getDefault() 返回 en 时无法找到与代码关联的正确消息,但在返回 en_US 时却能够找到正确的消息>
默认区域设置:en_US
key: CODE_1, Locale: Locale.US = Hit
key: CODE_1, Locale: Locale.CANADA = Hit
key: CODE_1, Locale: fr_CA = Hit
key: CODE_1, Locale: null (use default) = Hit
默认区域设置:en
key: CODE_1, Locale: Locale.US = Hit
key: CODE_1, Locale: Locale.CANADA = CODE_1
key: CODE_1, Locale: fr_CA = CODE_1
key: CODE_1, Locale: null (use default) = CODE_1
我只有一个包,其类路径中包含 CODE_1,即 message_en_US.properties
Why is the spring "ReloadableResourceBundleMessageSource" unable to find the proper message associated with a code when Locale.getDefault() returns en, but able to find the proper message when it returns en_US
Default Locale: en_US
key: CODE_1, Locale: Locale.US = Hit
key: CODE_1, Locale: Locale.CANADA = Hit
key: CODE_1, Locale: fr_CA = Hit
key: CODE_1, Locale: null (use default) = Hit
Default Locale: en
key: CODE_1, Locale: Locale.US = Hit
key: CODE_1, Locale: Locale.CANADA = CODE_1
key: CODE_1, Locale: fr_CA = CODE_1
key: CODE_1, Locale: null (use default) = CODE_1
I have only one bundle that has the CODE_1 in the classpath which is message_en_US.properties
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该消息仅针对
en_US
区域设置进行定义,因为它是在message_en_US.properties
文件中定义的。从语言环境en_US
(Locale.US) 切换到en
时出现不同行为的原因是 ReloadableResourceBundleMessageSource 默认情况下会执行 后备到系统区域设置,如果在请求的区域设置中找不到消息。在第一种情况下,当
en_US
为默认值时,总是会找到消息,因为请求的本地是en_US
,否则,消息源会进行回退到定义消息的en_US
。当默认区域设置为
en
时,只有en_US
查询有效,因为它在正确的属性文件中查找。对于所有其他消息,在相应的属性文件中找不到该消息,并且在后备语言环境en
中也找不到该消息。The message is only defined for the
en_US
locale, since it's defined in themessage_en_US.properties
file. The reason that you get different behaviour when switching from localeen_US
(Locale.US) toen
is that ReloadableResourceBundleMessageSource by default does a fallback to the system locale if a message is not found in the requested locale.In the first case, when
en_US
is the default, then the message is always found, since the the requested local isen_US
or when not, the message source does a fallback is toen_US
, where the message is defined.When the default locale is
en
, only theen_US
query works, since that is looking in the correct properties file. For all others, the message is not found in the corresponding properties file, and it is also not found in the fallback localeen
.这是因为它找不到较少的国家/语言特定属性文件
messages_en.properties
和messages.properties
。如果您希望拥有适用于所有
en
语言(无论位于哪个国家/地区)的默认捆绑包,那么您应该拥有messages_en.properties
。如果您希望拥有适用于所有语言的默认捆绑包,那么您应该拥有messages.properties
。实际上,
messages_en_US.properties
仅在您有多种英语方言(例如en_UK
和en_US
)时才有用,但是您实际上还应该提供另一个是另一种英语“方言”。其中之一应命名为messages_en.properties
,然后可以将其用作未指定国家/地区的访问者的“默认”英语包。It's because it cannot find less country/language-specific properties files
messages_en.properties
andmessages.properties
.If you'd like to have a default bundle for all
en
languages regardless of the country, then you should have amessages_en.properties
. If you'd like to have a default bundle for all languages, then you should have amessages.properties
.The
messages_en_US.properties
is in fact only useful whenever you have multiple English language dialects likeen_UK
anden_US
, but then you should really also supply the another one for the other English "dialect". One of them should be namedmessages_en.properties
which can then be used as "default" English bundle for visitors which are not specifying the country.