Togrul Sadigov
Togrul Sadigov

Reputation: 281

inline javascript get locale in Thymeleaf

I need current locale. I get locale code and send it to javascript. This code throw error:

<script type="text/javascript">
     var loc = [[${#locale}]];

Uncaught ReferenceError: ru_RU is not defined.

What is the problem? This one not throw error:

<div th:text="${#locale}"></div>

Upvotes: 1

Views: 959

Answers (1)

Michal Drozd
Michal Drozd

Reputation: 1351

Either add quotes so you have

var loc = "[[${#locale}]]";

or change type="text/javascript" to th:inline="javascript"

This is because [[${#locale}]] translated to ru_RU without quotes is considered as variable, not string, this is why you get ReferenceError

Upvotes: 1

Related Questions