简体   繁体   English

内联 javascript 在 Thymeleaf 中获取语言环境

[英]inline javascript get locale in Thymeleaf

I need current locale.我需要当前的语言环境。 I get locale code and send it to javascript.我获取区域设置代码并将其发送到 javascript。 This code throw error:此代码抛出错误:

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

Uncaught ReferenceError: ru_RU is not defined.未捕获的 ReferenceError: ru_RU 未定义。

What is the problem?问题是什么? This one not throw error:这一个不抛出错误:

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

Either add quotes so you have要么添加引号,以便您拥有

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

or change type="text/javascript" to th:inline="javascript"或将type="text/javascript"更改为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这是因为[[${#locale}]]翻译成没有引号的ru_RU被认为是变量,而不是字符串,这就是你得到 ReferenceError 的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM