6

In a form I have radio buttons that correspond to an enum:

public enum EleType {
    INTEGER, 
    CHARACTER
}

The html of the form is:

<div class="form-check" th:each="elementType : ${T(org.example.sorting.EleType).values()}">
    <input type="radio" class="form-check-input" name="elemType" th:id="${{elementType}}" th:field="*{elementType}" th:value="${{elementType}}" />
    <label th:for="${{elementType}}"  th:text="${{elementType}}">Elem Types</label>
</div>

This gives me radio buttons with labels "INTEGER", "CHARACTER".

I would like to translate these strings using the messages.properties file. So with label.pages.sorting.INTEGER=Integer and label.pages.sorting.CHARACTER=Character in the messages.properties file I tried:

<label th:for="${{elementType}}"  th:text="#{label.pages.sorting.${{elementType}}}">Elem Types</label>

which does not work since the ${{elementType}} is not evaluated inside the #{...}.

How can I make this work? Or is there another/better way to introduce localization of enum?

user1583209
  • 1,637
  • 3
  • 24
  • 42

2 Answers2

12

I figured it out myself. The following works:

<label th:for="${{elementType}}"  th:text="#{'label.pages.sorting.'+${{elementType}}}">Elem Types</label>
user1583209
  • 1,637
  • 3
  • 24
  • 42
0

I had to adapt the answer of @user1583209 to the following to make it work:

<label th:for="${elementType}"  th:text="#{'label.pages.sorting.'+${elementType}}">Elem Types</label>

Note the { and } instead of {{ and }}.

kistlers
  • 97
  • 5