【问题标题】:In Spring boot, I'm trying to create another .property file that functions like messages.property, Is that possible?在 Spring Boot 中,我正在尝试创建另一个功能类似于 messages.property 的 .property 文件,这可能吗?
【发布时间】:2021-10-07 11:32:24
【问题描述】:

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

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

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

  1. 这可能吗?
  2. 如果是,怎么做?
  3. 如果再次是,我可以像添加labels_ja.properties(日语)那样进行国际化吗?

【问题讨论】:

标签: spring-boot internationalization thymeleaf properties-file


【解决方案1】:

您可以通过配置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 中找到。

【讨论】:

  • 如果我将labels.properties 放在resources/new_folder/labels.properties 之类的文件夹中会怎样?我需要在 spring.messages.basename 中添加 new_folder/labels 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
相关资源
最近更新 更多