代码之家  ›  专栏  ›  技术社区  ›  Wim Deblauwe

用Thymeleaf翻译参数中的翻译

  •  0
  • Wim Deblauwe  · 技术社区  · 7 年前

    使用Thymeleaf 3.0.9,假设我有以下翻译:

    hello=Hello {0}
    

    在我的模板中,我有:

    <span th:text="#{hello(${user.getName()})}"></span>
    

    现在假设 user.getName() null ,所以我想在这种情况下展示一些漂亮的东西:

    <span th:text="#{hello(${user.getName()?:'Unknown'})}"></span>
    

    Unknown ,我该怎么做?

    我试过:

    <span th:text="#{hello(${user.getName()?:#{unknown})}"></span>
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   holmis83    7 年前

    我想你可以用 #messages 实用程序对象:

    <span th:text="#{hello(${user.getName()?:#messages.msg('unknown')})}"></span>
    

    unknown=Unknown