在 Spring 引导中,我试图创建另一个 .属性 文件,其功能类似于 messages.property,这可能吗?

In Spring boot, I'm trying to create another .property file that functions like messages.property, Is that possible?

所以我正在尝试创建另一个我可以在 Thymeleaf 中使用的 .属性 文件(称为 labels.properties):

<div th:text=#{property.from.labels}></div>

现在我只是在资源文件夹的根目录中添加 labels.properties,目前它不起作用。这样做的目的是我想将处理错误消息的 属性 文件与标签和按钮的文本分开。

  1. 这可能吗?
  2. 如果是,怎么做?
  3. 如果又是,我可以做国际化,比如添加 labels_ja.properties(日语)吗?

您可以通过配置 spring.messages.basename 属性 自定义消息包的命名(和位置)。例如:

spring.messages.basename=labels

通过这样做,Spring 将在 classpath:labels.propertiesclasspath:labels_{locale}.properties 中查找邮件,例如 classpath:labels_ja.properties.

如果你想在常规 messages.properties 之外使用它,你可以使用逗号分隔值:

spring.messages.basename=messages,labels

逗号分隔列表中的第一个将优先于其他列表。如果您在 messages.propertieslabels.properties 中都有 property.from.labels,则在本例中将选择 messages.properties 中的那个。

这也可以在 the documentation about internationalization 中找到。