Armando Pérez
Armando Pérez

Reputation: 182

Thymeleaf compare #locale expression object with string

I want to set a th:class attribute depending on the context locale using the expression object #locale.

I have tried

th:class="${#locale}=='en'?'active':''"
th:class="${#locale=='en'}?'active':''"

Both of them results in false, but whent I print it with th:text="${#locale}, I got the correct locale code (en,es).

Any idea of how to compare the #locale object with a locale code?

Upvotes: 4

Views: 7007

Answers (3)

asifaftab87
asifaftab87

Reputation: 1443

I used like this

th:text="${#locale.toString()}=='in'?'active':'inactive'"

Upvotes: 0

Armando Pérez
Armando Pérez

Reputation: 182

Based on the answer posted by David_Garcia, I could resolve my issue this way:

th:class="__${#locale}__=='en'?'active':''

Upvotes: 12

David_Garcia
David_Garcia

Reputation: 654

This is a issue that I told to the guys of thymeleaf time ago.

You need to resolve first the #locale before comparing it with "en". You can do that adding 2 underscore at the beggining and end to the expresion that you want to resolve first. In your case will be something like this:

th:call="$({__#locale__}=='en'?'active':'')"

Upvotes: 4

Related Questions