在 Spring 启动中,我正在尝试创建另一个功能类似于 messages.property 的 .property 文件,这可能吗?



所以我正在尝试创建另一个.properties文件(称为labels.properties),我可以在Thymelaf中使用,如下所示:

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

现在我只是在resource文件夹的根目录中添加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中的一个。

这也可以在国际化部分的Spring Boot参考文档中找到。

相关内容

最新更新