为什么 Spring 会加载与指定语言不同的消息?

Why does Spring load messages for a different language than specified?

我创建了一个简单的 Spring 启动应用程序,用于将消息打印到标准输出。这是主要的 class:

@SpringBootApplication
public class I18nTestApplication {

    public static void main(String[] args) {
        final ApplicationContext ctx = SpringApplication.run(I18nTestApplication.class, args);
        final Locale locale = Locale.US;

        System.out.println(ctx.getMessage("test", null, locale));
    }

}

资源文件夹包含两个消息文件:

messages.properties:

test=This is English

messages_de.properties:

test=Das ist Deutsch

预期的程序输出是:This is English 但它总是打印 Das ist Deutsch 作为输出。即使我设置了任何语言环境,程序也总是打印德语消息作为输出。这里发生了什么?我弄错了吗?

根据你的姓名和你在个人资料页面中的位置,我假设你的系统语言环境是德语。

用于查找合适资源包的算法主要包括

  • 正在为所请求的语言环境查找捆绑包
  • 如果未找到,则回退到系统区域设置的包
  • 如果仍未找到,则回退到默认包

更多信息见the documentation

提供一个名为 messages_en.properties 的文件(即使是空的:密钥也会在父包中查找),这将按预期工作。