java - Thymeleaf:从 Java 代码访问外部字符串

标签 java maven thymeleaf

我目前在一个经典的 Maven 项目(没有 Spring)中使用 Thymeleaf,并且我在文档中找不到一种简单的方法来从 Java 代码访问外部化国际化字符串,例如 Rails' t() 函数。

我找到了the following教程,但对于我想要做的事情来说,它确实似乎过度设计。

最佳答案

Thymeleaf 文档 ( http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#a-multi-language-welcome ) 表示默认配置是使用属性文件。

This standard message resolver expects to find messages for /WEB-INF/templates/home.html in .properties files in the same folder and with the same name as the template, like:

  • /WEB-INF/templates/home_en.properties for English texts.
  • /WEB-INF/templates/home_es.properties for Spanish language texts.
  • /WEB-INF/templates/home_pt_BR.properties for Portuguese (Brazil) language texts.
  • /WEB-INF/templates/home.properties for default texts (if locale is not matched).

属性文件 home_es.properties 有一个类似的条目

home.welcome=¡Bienvenido a nuestra tienda de comestibles!

你可以这样使用它:

<p th:text="#{home.welcome}">Welcome to our grocery store!</p>

关于java - Thymeleaf:从 Java 代码访问外部字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31424398/

相关文章:

java - Jsoup、java、android studio - 没有结果

java - Maven javadoc 插件 - 包不存在

Android Studio 在构建期间每次都会下载 maven 元数据

javascript - 从 javascript 或 jquery 更新 spring 模型值

java - 工厂创建实例的 AutoCloseable "resource leak"警告?

java - Java 中的函数对象

java - 是否有一些 Java api 将复杂的 JSON 存储到 Cassandra?

java - Jenkinsfile (Declarative Pipeline) for Java/maven/Spring Boot

java - thymeleaf 的不同行为(通过数据库和resttemplate)

java - 带有 thymeleaf 国际化的 Spring 4 无法识别来自资源属性文件的消息