Search code examples
thymeleaf

(th:text) How concat a Message (i18n) expressions with other text


I am working with:

  • Thymeleaf 3.0.9.RELEASE
  • Spring Framework

About i18n the following works fine (within any html component):

th:text="#{person.name.label}"

The person.name.label key exists within a .properties file and according with a Locale value, for example Name is printed.

I need print now instead Name:. Observe the :, it for example to be used for a <label>

If I try:

th:text="#{person.name.label}:"

I get

org.thymeleaf.exceptions.TemplateProcessingException: 
Could not parse as expression: "#{persona.name.label}:"

If I try

th:text="#{person.name.label:}"

The ??persona.name.label:_en_US?? is printed instead.

I don't want add : directly within the .properties file because I want use person.name.label in a <th> within the <thead>, thus there is not necessary print :

How is the correct approach?.


Solution

  • Standard syntax for appending text.

    th:text="#{person.name.label} + ':'"
    

    Or, you may be interested in literal substitution.

    th:text="|#{person.name.label}:|"
    

    You can also accomplish these kinds of expressions with inlining. This is enabled by default in Thymeleaf 3, and the expressions look like this.

    <span>[[#{person.name.label}]]:</span>
    

    In Thymeleaf 2, you have to enable this feature by adding th:inline, like this:

    <span th:inline="text">[[#{person.name.label}]]:</span>