11

I am making a language learning web app that when you hover over the word, it pronounces it for you. I'd like to access the native speaker translations from Google Translate API.

I've found this resource which gives http://translate.google.com/translate_tts as the base URL and tl for target language and q for the query string.

This works awesome when I just access it in the browser, http://translate.google.com/translate_tts?tl=zh-CN&q=你好, but any httprequests for my app return a 403 Forbidden error.

localhost:~ me$ wget "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+world" --2015-06-02 11:02:06-- http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+world Resolving translate.google.com... 173.194.123.38, 173.194.123.36, 173.194.123.32, ... Connecting to translate.google.com|173.194.123.38|:80... connected. HTTP request sent, awaiting response... 403 Forbidden 2015-06-02 11:02:07 ERROR 403: Forbidden.

Is there a formal Google API for text-to-speech associated with their payment plans for traditional Google Translate API that I'm just not finding? Or is there a way to get and play this audio somehow?

user3871
  • 12,432
  • 33
  • 128
  • 268

5 Answers5

28

You can get anything sound, I found it: https://translate.google.com.vn/translate_tts?ie=UTF-8&q=ANYTHING_TEXT&tl=en&client=tw-ob Note: client=tw-ob

Tan Nguyen
  • 386
  • 4
  • 9
  • 1
    How did you find this? – AtilioA Jan 15 '20 at 19:16
  • 3
    Dude you are a legend! – Adnane Ar May 20 '20 at 20:36
  • 1
    @AtilioA you can find it by using Chrome's Network web tool. It logs the request when you press the text-to-speech button. If it doesn't log, you need to press the gray button on top left corner on the web tools. It will turn red and will start recording. – bapap Jan 22 '21 at 18:38
5

That resource is dated. Google deprecated free access to the language translate api. Now an API key is needed. The api is also now in V2.

Without an api key you will receive the 403 forbidden error.

More information here: https://cloud.google.com/translate/docs

Tarang
  • 75,157
  • 39
  • 215
  • 276
  • 2
    Thank you. I've looked at their API docs and didn't see any endpoints for getting text-to-speech files. Am I missing that? I'd be willing to pay if I can access the audio. Btw, you still can download the audio from the deprecated `translate_tts` URI using `wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+world"` – user3871 Jun 02 '15 at 15:12
  • @Growler it's not an official api but still takes an api key. You can try get passed it using techniques from here: http://stackoverflow.com/questions/12883330/request-to-google-text-to-speech-api. You're likely to get blocked often once you make requests with over 100 characters. – Tarang Jun 02 '15 at 15:14
  • @Growler I've seen that, you're likely to get blocked using it once Google detects how youre using the requests. – Tarang Jun 02 '15 at 15:15
  • That SO post contains deprecated techniques that I used original when I posted this. The most updated technique there is from http://weston.ruter.net/2009/12/12/google-tts/ – user3871 Jun 02 '15 at 15:18
  • @Growler that SO post is kept up to date and the link youve mentioned is from that post which still does work. – Tarang Jun 02 '15 at 15:19
  • @Growler You will stil need to hide your referrer as mentioned in the SO post. – Tarang Jun 02 '15 at 15:20
  • That would help bypass the `403` errors on HTTP request that I've been getting? How would I structure my query/code to hide the referrer? The goal here is to use the text-to-speech audio in my app when a user hovers over the word. – user3871 Jun 02 '15 at 15:21
  • @Growler You would hide the referrer using the library you use to make the HTTP request to fetch that mp3 file by making sure the referrer header does not exist. Also keep in mind they still bock requests once they detect it is being used for an API. You can't realistically create a stable app off this without an api key. – Tarang Jun 02 '15 at 15:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79435/discussion-between-growler-and-akshat). – user3871 Jun 02 '15 at 15:31
  • I've gotten an API key and reposted here: http://stackoverflow.com/questions/30929518/google-translate-api-no-access-control-origin-with-text-to-speech. If I have an API key, do I still need to hide referrer header? If so, how? – user3871 Jun 19 '15 at 03:49
1

You are only allowed to use on google translate domain by web tools:

var elText = 'こんにちは世界';
var language = 'ja-JP';
var source = 'https://translate.google.com/translate_tts?tl=' +
    language +
    '&q=' +
    encodeURIComponent(elText) +
    '&client=tw-ob';
var audio = new Audio(source);
audio.play();
Juan
  • 17
  • 3
0
<!DOCTYPE html>
<html>
<head>
<title>Text TO Speach</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#says").click(function(){
    var url="https://translate.google.com/translate_tts?ie=UTF-8&q="+encodeURIComponent($("#text").val())+"&tl=ta&client=tw-ob";
    $(".speech").attr('src',url).get(0).play();
    console.log("URL : "+$(".speech").attr('src'));
  });
  
});
</script>
</head>
<body>
<input type="text" name="text" id="text" value="Token Number : 2">
<button type="button" id="says">Speak</button>
<audio src="" class="speech" hidden></audio>
</body>
</html>
0
    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    </head>
    <body>
    
    <input type="text" name="text" id="text" value="Token Number : 2">
    <button type="button" id="says">Speek</button>
    <audio src="" class="speech" hidden></audio>
    <script>
      $("#says").click(function(){
         if ('speechSynthesis' in window) {
// Speech Synthesis supported 
          var msg = new SpeechSynthesisUtterance();
    msg.text = $("#text").val();
    window.speechSynthesis.speak(msg);
    
    }else{
      // Speech Synthesis Not Supported 
      alert("Sorry, your browser doesn't support text to speech!");
    }
    });
    </script>
    </body>
    </html>