Anmol Yousaf
Anmol Yousaf

Reputation: 75

IO Exception using java-google-translate-text-to-speech Api

I am having issues using java-google-translate-text-to-speech.Trying to translate a language to another. This is my code:

import com.gtranslate.Language;
import com.gtranslate.Translator;
import java.io.IOException;

public class Main {
       public static void main(String[] args){
               Translator translate = Translator.getInstance();
               String text = translate.translate("Hello", Language.ENGLISH,Language.PORTUGUESE);
               System.out.println(text); 
              }
          }

Its giving me an error:

 java.io.IOException: Server returned HTTP response code: 503 for URL: http://ipv4.google.com/sorry/IndexRedirect?continue=http://translate.google.com.br/translate_a/t%3Fclient%3Dt%26text%3DHello%26hl%3Den%26sl%3Den%26tl%3Den%26multires%3D1%26prev%3Dbtn%26ssel%3D0%26tsel%3D0%26sc%3D1&q=CGMSBHqsFhAY_L3FqQUiGQDxp4NLxnAO-gsMAyd56ktUpufqNjEC280

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1459)
at com.gtranslate.utils.WebUtils.source(WebUtils.java:24)
at com.gtranslate.parsing.ParseTextTranslate.parse(ParseTextTranslate.java:19) 

Upvotes: 1

Views: 2367

Answers (2)

Just found a simple 2-step solution. Please see Comment #4 at the following URL (requires only a minor modification of the sources):

https://code.google.com/p/java-google-translate-text-to-speech/issues/detail?id=8

STEP 1 in Comment #4 is straightforward. Let me cite it from the above webpage:

In class com.gtranslate.URLCONSTANT change public static final String GOOGLE_TRANSLATE_TEXT = "http://translate.google.com.br/translate_a/t?";

TO public static final String GOOGLE_TRANSLATE_TEXT1 = "http://translate.google.com.br/translate_a/single?";

...however in STEP 2 it is much simpler just to add a &dt=t URL parameter-value pair at the end of the generated URL in the com.gtranslate.parsing.ParseTextTranslate.appendURL() method.

...the original STEP 2 in Comment #4 above was the following, I cite (FYR):

STEP2) In the class, the appendURL function needs to be changed as shown com.gtranslate.parsing.ParseTextTranslate @Override public void appendURL() { Text input = textTranslate.getInput(); Text output = textTranslate.getOutput(); url = new StringBuilder(URLCONSTANTS.GOOGLE_TRANSLATE_TEXT); /* url.append("client=t&text=" + input.getText().replace(" ", "%20")); url.append("&hl=" + input.getLanguage()); url.append("&sl=" + input.getLanguage()); url.append("&tl=" + output.getLanguage()); url.append("&multires=1&prev=btn&ssel=0&tsel=0&sc=1"); */

        url = new StringBuilder(URLCONSTANTS.GOOGLE_TRANSLATE_TEXT); 

url.append("client=t&sl=auto&tl="+ output.getLanguage()
                +"&hl=" + input.getLanguage()
                +"&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&otf=1&rom=1&ssel=0&tsel=3&kc=1&tk=620730|996163"
                + "&q=" + input.getText().replace(" ", "%20"));

}

.........end of citation...and sooooooooo, for example, just replace this line in the appendURL() method:

url.append("&multires=1&prev=btn&ssel=0&tsel=0&sc=1");

...to this:

url.append("&multires=1&prev=btn&ssel=0&tsel=0&sc=1&dt=t");

Additionally here are some values for the dt URL param, which practically specifies what to return in the reply:

  • t - translation of source text
  • at - alternate translations
  • rm - transcription / transliteration of source and translated texts
  • bd - dictionary, in case source text is one word (you get translations with articles, reverse translations, etc.)
  • md - definitions of source text, if it's one word
  • ss - synonyms of source text, if it's one word
  • ex - examples
  • ...

P.S.: A similar HTTP 503 error happens with Google TTS (due to the background API change). You can find the solution to that problem here: Text to Speech 503 and Captcha Now

Upvotes: 1

Saagar Elias Jacky
Saagar Elias Jacky

Reputation: 2688

HTTP Response Code: 503 : Service Unavailable says : The server is currently unavailable may be because it is overloaded or down for maintenance.

The server might currently be unable to handle the request due to a temporary overloading or maintenance of the server.

Note: Some servers may simply refuse the connection and might result in 503 response

Upvotes: 0

Related Questions