Click here to Skip to main content
15,897,704 members

how to make translate by a Button

markohack asked:

Open original thread
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
<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>
JavaScript
function translateText(response) {
  document.getElementById("translation").textContent += "" + response.data.translations[0].translatedText;
}
HTML
</script>
<script>
JavaScript
 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);
HTML
   </script>
  </body>
</html>

[Edit]Code block added[/Edit]
Tags: Javascript, HTML

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900