Thymeleaf可以像Freemarker一样进行本地化的模板查找吗?

ber*_*nie 9 spring freemarker spring-mvc thymeleaf spring-boot

Freemarker(默认情况下)使用区域设置来构建它在加载和包含模板时查找的文件名.例如,tos.ftl使用en_US区域设置加载(模板)将查找:

  1. tos_en_US.ftl
  2. tos_en.ftl
  3. tos.ftl

当页面在不同语言之间完全不同时,这对于翻译整个页面非常有用.例如,"服务条款"页面可能主要是静态的,因此不同的语言将具有完全不同的内容.在这种情况下,将整个内容外部化到从消息包加载的消息是一件麻烦事.

我现在正在学习Thymeleaf,无法找到有关类似功能的任何信息.我知道Thymeleaf使用本地化的消息包来填充th:text元素,但是它可以加载模板文件的本地化版本吗?

注意:我正在使用Spring Boot

san*_*uck 1

Thymeleaf 的行为与Spring 4 MVC 国际化相同(我猜你将 Thymeleaf 与 Spring 一起使用??),它用来messages.properties实现这一点。例如,您有一个包含#{hello}消息的模板:

<html xmlns="http://www.w3.org/1999/xhtml" 
  xmlns:th="http://www.thymeleaf.org">
<head>
    <title></title>
</head>
<body>
    <span th:text="#{hello}">
</body>
Run Code Online (Sandbox Code Playgroud)

#{hello}text 将绑定到messages.propertiesproperty hello

如果您locale是另一个,例如ru_RU您只需添加messages_ru_RU.properties和更改locale您的应用程序。

之后,将从本地化属性文件中获取您的消息。 请注意,如果您使用本地化消息文件,则必须messages.properties文件。