问题
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?
回答1:
Based on the answer posted by David_Garcia, I could resolve my issue this way:
th:class="__${#locale}__=='en'?'active':''
回答2:
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':'')"
回答3:
I used like this
th:text="${#locale.toString()}=='in'?'active':'inactive'"
来源:https://stackoverflow.com/questions/30484119/thymeleaf-compare-locale-expression-object-with-string