Search code examples
thymeleaf

How to pass message.properties value to a String utility method in Thymeleaf


In my message.properties I have:

some.text=This text is for example

I would like to use one of Thymeleaf's String utility methods in my html for example:

<p th:text="${#strings.toUpperCase(#{some.text})}"></p>

But this line obviously doesn't work as I get an error. I don't seem to know how to pass some.text to a String utility method in Thymeleaf.

I couldn't find any similar examples in the documentation.
Can someone with more experience explain what I'm doing wrong?


Solution

  • I assume the standard message (without the uppercase function wrapped around it) is already working OK for you.

    Instead of using that standard way to show messages...

    #{some.text}
    

    ... use this alternative function-based syntax:

    ${#messages.msg('some.text')}
    

    So now, when you place it inside the uppercase function, it will be this:

    <p th:text="${#strings.toUpperCase(#messages.msg('some.text'))}"></p>
    

    For some reason, Thymeleaf will not let you use a standard message expression as the parameter to one of its utility methods - not even with the __${...}__ preprocessor.

    So instead, use a message function inside the uppercase function.