谷歌翻译免费 api 协助



我对javascript和构建网站有点陌生,我大部分时间都在编写c#。 我正在尝试构建一些东西,我需要使用谷歌翻译 api,这个问题需要花钱,所以我更喜欢使用免费的 API,所以我找到了这个。

https://ctrlq.org/code/19909-google-translate-api

所以我稍微改变了一下,独自尝试,因为我不确定e类型。 所以这是我的代码:

function doGet(text) {
var sourceText = text;
var translatedText = LanguageApp.translate('en', 'iw', sourceText);
var urllog = "https://translate.googleapis.com/translate_a/single?client=gtx&sl="
+ "en" + "&tl=" + "iw" + "&dt=t&q=" + encodeURI(text);
var result = JSON.parse(UrlFetchApp.fetch(urllog).getContentText());
translatedText = result[0][0][0];
console.log(translatedText); 
}

所以 URL 正在为我下载一个名为">f.txt"的文本文件,其中包含翻译代码,问题是我不希望它下载文件,

我只需要它给我的 txt 文件中的翻译, 问题是我不确定如何在javascript变量中获取该信息,而且我不希望它也给我该文件。

那么我该如何阅读它呢?如何在不下载文件的情况下使用该文件,以及如何将其推送到字符串变量?我如何取消下载并仅获取翻译?

谢谢!

顺便一提 如果有人知道我在链接上显示的功能doGet(e),"e"是什么?函数想要什么?

我知道我迟到了一年,但我遇到了同样的问题并使用 PHP 修复了它。我创建了这个简单的PHP函数:

function translate($text, $from, $to) {
if($text == null)
echo "Please enter a text to translate.";
if($from == null)
$from = "auto";
if($to == null)
$to = "en";
$NEW_TEXT = preg_replace('/s+/', '+', $text);
$API_URL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" . $from . "&tl=" . $to . "&dt=t&q=" . $NEW_TEXT;
$OUTPUT = get_remote_data($API_URL);
$json = json_decode($OUTPUT, true); // decode the JSON into an associative array
$TRANSLATED_OUTPUT = $json[0][0][0];
echo $TRANSLATED_OUTPUT;    
}

示例用法(英语到西班牙语(:

translate("Hello", "en", "es"); //Output: Hola
/*
sourceLanguage: the 2-3 letter language code of the source language (English = "en")
targetLanguage: the 2-3 letter language code of the target language (Hebrew is "iw")
text: the text to translate
callback: the function to call once the request finishes*
* Javascript is much different from C# in that it is an asynchronous language, which 
means it works on a system of events, where anything may happen at any time
(which makes sense when dealing with things on the web like sending requests to a 
server). Because of this, Javascript allows you to pass entire
functions as parameters to other functions (called callbacks) that trigger when some 
time-based event triggers. In this case, as seen below,
we use our callback function when the request to google translate finishes.
*/
const translate = function(sourceLanguage,targetLanguage,text,callback) {
// make a new HTTP request
const request = new XMLHttpRequest();
/*
when the request finishes, call the specified callback function with the 
response data
*/
request.onload = function() {
// using JSON.parse to turn response text into a JSON object
callback(JSON.parse(request.responseText));
}
/*
set up HTTP GET request to translate.googleapis.com with the specified language 
and translation text parameters
*/
request.open(
"GET",
"https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + 
sourceLanguage + "&tl=" + targetLanguage + "&dt=t&q=" + text,
true
);
// send the request
request.send();
}

/*
translate "This shouldn't download anything" from English to Hebrew
(when the request finishes, it will follow request.onload (specified above) and 
call the anonymous 
function we use below with the request response text)
*/
translate("en","iw","This shouldn't download anything!",function(translation) {
// output google's JSON object with the translation to the console
console.log(translation);
});

相关内容

  • 没有找到相关文章

最新更新