Thymeleaf compare #locale expression object with string

霸气de小男生 提交于 2019-12-19 10:37:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!