如何通过按钮进行翻译 [英] how to make translate by a Button
问题描述
大家好我有翻译google api的代码,但只有当我在scurce代码中输入文本时才翻译,但我想通过按钮翻译任何文字,请知道谁写信给我)
Hi guys I have code for translator google api but the translate only when i''m enter text in scurce code,but i want to translate any text by button please who know write me)
<html>
<head>
<title>Translate API Example</title>
</head>
<body>
<textarea rows="4" cols="50" id="sourceText" >
Welcome in my site
</textarea>
<textarea rows="4" cols="50" id="translation" >
</textarea>
<input name="send" type="button" onClick="newScrip" value="translate">
<script>
function translateText(response) {
document.getElementById("translation").textContent += "" + response.data.translations[0].translatedText;
}
</script>
<script>
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
var sourceText = escape(document.getElementById("sourceText").textContent);
// WARNING: be aware that YOUR-API-KEY inside html is viewable by all your users.
// Restrict your key to designated domains or use a proxy to hide your key
// to avoid misuage by other party.
var send = 'https://www.googleapis.com/language/translate/v2?key=My Key&source=en&target=iw&callback=translateText&q=translate+is+fast' + sourceText;
newScript.src = send;
// When we add this script to the head, the request is sent off.
document.getElementsByTagName('textarea')[0].appendChild(newScript);
</script>
</body>
</html>
添加了代码块[/ Edit]
Code block added[/Edit]
推荐答案
我注意到你没有在这行中设置密钥:
I notice you have not set the key in this line:
var send = 'https://www.googleapis.com/language/translate/v2?key=My Key&source=en&target=iw&callback=translateText&q=translate+is+fast' + sourceText;
Google翻译API不再免费:https://developers.google.com/translate/v2/pricing [ ^ ]所以你需要注册(获得我认为的钥匙)。
至于其余的代码,我不知道它是否会起作用。您将获得更多关于Google开发者群体的好运,因为他们将有一个专门用于翻译API。
The Google translate API is no longer free: https://developers.google.com/translate/v2/pricing[^] so you''ll need to register (to get a key I assume).
As for the rest of the code, I don''t know whether it will work or not. You will have more luck on Google developer groups as they''ll have one dedicated to the translate api.
这篇关于如何通过按钮进行翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!