当默认区域设置为“en”时,ReloadableResourceBundleMessageSource 无法找到消息

发布于 2024-09-10 19:08:40 字数 762 浏览 3 评论 0原文

为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒冷纷飞旳雪 2024-09-17 19:08:40

该消息仅针对 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 the message_en_US.properties file. The reason that you get different behaviour when switching from locale en_US (Locale.US) to en 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 is en_US or when not, the message source does a fallback is to en_US, where the message is defined.

When the default locale is en, only the en_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 locale en.

未央 2024-09-17 19:08:40

这是因为它找不到较少的国家/语言特定属性文件 messages_en.propertiesmessages.properties

如果您希望拥有适用于所有 en 语言(无论位于哪个国家/地区)的默认捆绑包,那么您应该拥有 messages_en.properties。如果您希望拥有适用于所有语言的默认捆绑包,那么您应该拥有messages.properties

实际上,messages_en_US.properties 仅在您有多种英语方言(例如 en_UKen_US)时才有用,但是您实际上还应该提供另一个是另一种英语“方言”。其中之一应命名为 messages_en.properties,然后可以将其用作未指定国家/地区的访问者的“默认”英语包。

It's because it cannot find less country/language-specific properties files messages_en.properties and messages.properties.

If you'd like to have a default bundle for all en languages regardless of the country, then you should have a messages_en.properties. If you'd like to have a default bundle for all languages, then you should have a messages.properties.

The messages_en_US.properties is in fact only useful whenever you have multiple English language dialects like en_UK and en_US, but then you should really also supply the another one for the other English "dialect". One of them should be named messages_en.properties which can then be used as "default" English bundle for visitors which are not specifying the country.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文