我是javascript和构建网站的新手,我大多数时候都在编程#。 我正在尝试构建一些东西,我需要使用google translate api,这是一个花钱的问题,所以我更喜欢使用Free 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);
}
所以网址下载了一个名为" f.txt "的文本文件。包括翻译代码的问题是我不想让它下载文件,
我只需要它给我的txt文件中的翻译, 问题是我不知道如何在javascript变量中获取该信息,而且我也不想让它给我那个文件..
那我怎么读呢? 如何在不下载文件的情况下使用该文件,如何将其推送到字符串变量? 我如何取消下载并只获得翻译?
谢谢!
顺便说一下 如果有人知道我在链接上显示的功能doGet(e)
,那么" e"?这个功能想要什么?
答案 0 :(得分:1)
我知道我迟到了一年,但是我遇到了同样的问题,并使用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
答案 1 :(得分:-1)
/*
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);
});