我目前在一个经典的 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/