gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:37:36 25 4
gpt4 key购买 nike

Freemarker(默认)uses the locale to build the file names it looks for加载和包含模板时。例如,加载 tos.ftl (模板)与 en_US语言环境会寻找:

  • tos_en_US.ftl
  • tos_en.ftl
  • tos.ftl

  • 当页面在不同语言之间完全不同时,这对于翻译整个页面很有用。例如,“服务条款”页面可能大部分是静态的,因此不同的语言会有完全不同的内容。在这种情况下,将整个内容外部化为从消息包加载的消息是一件麻烦事。

    我现在正在学习 Thymeleaf,找不到任何关于类似功能的信息。我知道 Thymeleaf 使用 localized message bundles填写 th:text元素,但它可以加载 的本地化版本吗?模板文件 ?

    注意:我使用的是 Spring Boot

    最佳答案

    Thymeleaf 的行为与 Spring 4 MVC Internationalization 相同(我猜你在 Spring 中使用 Thymeleaf??),它使用 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>
    #{hello}文本将绑定(bind)到 messages.properties属性(property) hello .

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

    之后,您的消息将从本地化的属性文件中获取。
    注意一定要拥有 messages.properties如果您使用本地化消息文件,则为文件。

    关于spring - Thymeleaf 可以像 Freemarker 一样进行本地化模板查找吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36295677/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com