Search code examples
springspring-bootthymeleafspring-thymeleaf

Thymeleaf: Default message from messageKey


I want to set the default message of a translation. But I can only do this by writing the text directly into the template.

The translation of the default message can be done with #{default-message}. I can't find anything about this in the documentation.

<span th:text="${#messages.msgOrNull('') ?: 'My default Message'}"/>
// Output: "My default Message"

I have made many attempts. For example, the following. But it always returns a 500 or is not transformed.

<span th:text="${#messages.msgOrNull('') ?: #{default-message}}"/>

Solution

  • You can't nest expressions like this: ${... #{...}} -- move the second expression outside the first, like this:

    <span th:text="${#messages.msgOrNull('')} ?: #{default-message}"/>
    

    You can see other examples of these kinds of expressions in the Thymeleaf documentation.