Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization of enum in Spring Thymeleaf

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?

like image 788
user1583209 Avatar asked Nov 02 '16 16:11

user1583209


1 Answers

I figured it out myself. The following works:

<label th:for="${{elementType}}"  th:text="#{'label.pages.sorting.'+${{elementType}}}">Elem Types</label>
like image 199
user1583209 Avatar answered Sep 27 '22 23:09

user1583209