Click here to Skip to main content
13,141,354 members (88,614 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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]
Posted 28-Dec-12 1:44am
Updated 28-Dec-12 1:58am
ProgramFOX204.5K
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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;


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.
  Permalink  
Comments
markohack 28-Dec-12 8:59am
   
I have a key and it work but i want translated entry text,HOW?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100