Search code examples
spring-bootthymeleaf

How to show localization messages with parameters as links in Thymeleaf


I am aware from this question that you can add parameters to messages like this:

<p th:text="#{messages.msg1(${param1})}"></p>

However, I need to be able to make this parameter a link. Is this possible?

For example:

# messages.properties
msg1=Hello, my name is {0}!

# messages_x.properties
msg1={0}, my name is!

Hello, my name is Kim

Kim, my name is


Solution

  • It is possible by using th:utext and adding the link in the translation. Might not be ideal, but it should work:

    msg1=Hello, my name is <a href="...">{0}</a>!
    

    And:

    <p th:utext="#{messages.msg1(${param1})}"></p>
    

    (Note the utext instead of text, see documentation)