我对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"?这个函数想要什么?
发布于 2018-08-18 18:18:11
我知道我迟到了一年,但我也遇到了同样的问题,并使用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发布于 2018-12-19 22:20:57
/*
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);
});https://stackoverflow.com/questions/45908629
复制相似问题