【发布时间】:2020-11-10 23:27:33
【问题描述】:
我有一个 API 向提供商请求的服务列表。我无权以任何方式访问后端配置。我只能通过 twig 模板对 html 进行一些更改。
所以,我想知道是否有任何方法可以通过 ajax 与 google 自动翻译这些描述,或者我需要一个 api 等。
我已经检查了使用 google API 的可能性,但我没有足够的技术知识来实现它。
例子
<select class="form-control" id="category">
</select>
<div id="category_description"> <!-- <div> that i would like to translate-->
<p>Hello World!</p>
<p>Hello World!</p>
<p>Hello World!</p>
</div>
寻找国际化(i18n),寻找react-i18next之类的库,但据我了解,翻译需要更早,这变得不可行,因为有超过1500个描述和100万个字符。
所以,我想,我可能错了,如果这个翻译是动态的,它会更好,只是为了向用户显示的描述。
使用rapidapi的systran api,我可以使用那里提供的代码进行翻译,但我不知道如何使用输出来替换原文。
translate = $('#category_description').text()
var settings = {
"async": true,
"crossDomain": true,
"url": "https://systran-systran-platform-for-language-processing-v1.p.rapidapi.com/translation/text/translate",
data: {
source: "en",
target: "pt",
input: translate,
},
"method": "GET",
"headers": {
"x-rapidapi-host": "systran-systran-platform-for-language-processing-v1.p.rapidapi.com",
"x-rapidapi-key": "7f58b5667bmshd95c9dc930cbf6ap1d0268jsnd64f23091817"
},
}
$.ajax(settings).done(function (response) {
console.log(response)
});
输出:
Object {
outputs: [Object {
output: "
Olá! mundo!
Olá! mundo!
Olá! mundo!
",
stats: Object {
elapsed_time: 26,
nb_characters: 36,
nb_tokens: 9,
nb_tus: 3,
感谢您的帮助
【问题讨论】:
标签: javascript translation google-translate translate