简体   繁体   English

在JavaScript中使用Thymeleaf进行翻译

[英]Translation with Thymeleaf in JavaScript

In my HTML page I'm loading my translation with Thymeleaf like that: 在我的HTML页面中,我正在用Thymeleaf加载我的翻译,如下所示:

<button type="button" ng-click="$ctrl.resetData()" th:text="#{general.reset}">Reset</button>

Is it possible to translate text wich is hard coded in my JavaScript code? 是否可以翻译在我的JavaScript代码中硬编码的文本? 'Loading Data...' should be translated with th:text="#{general.loadingData}" <- but this is the way I would translate it im my HTML page, this doesn't work for JavaScript Code. “正在加载数据...”应使用th:text =“#{general.loadingData}” <-进行翻译,但这是我将其翻译到HTML页面中的方式,这不适用于JavaScript代码。 All the translations are in a messeges_en.properties file. 所有翻译都在messeges_en.properties文件中。 How can I translate the text in JavaScript? 如何翻译JavaScript中的文字?

function(request: any) {
    asLoading.show('Loading Data...');
    return request || $q.when(request);
}

You should use javascript Intl API . 您应该使用javascript Intl API

FormatJs is a modular collection of JavaScript libraries for internationalization that are focused on formatting numbers, dates, and strings for displaying to people. FormatJs是用于国际化的JavaScript库的模块化集合,其重点是格式化数字,日期和字符串以显示给人们。

It has support for ICU message format (International Components for Unicode) which is also well supported in java as well. 支持 ICU消息格式 (Unicode的国际组件),Java也很好地支持该消息格式

So you can expose your translations in ICU message format and consume them from both java and javascript. 因此,您可以采用ICU消息格式公开翻译内容,并从Java和javascript中使用它们。

In general, it is better to treat javascript and java code as separate applications as opposed to treating javascript as something that is tacked on top of a java template language like thymeleaf. 通常,最好将javascript和java代码视为单独的应用程序,而不是将javascript视为附加在诸如thymeleaf的java模板语言之上的东西。

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

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