Thursday, March 17, 2011

Tutorial: Using Google Translate API In Android

main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>



Translator.java:


package com.myapps.translate;


import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;


import com.google.api.translate.Language;
import com.google.api.translate.Translate;


public class Translator extends Activity {
    /** Called when the activity is first created. */
String translatedText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView tv = new TextView(this);
        Translate t = new Translate();
        
        try {
translatedText = t.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);


//tv.setText(translatedText);
        } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


        Toast.makeText(this, translatedText, Toast.LENGTH_SHORT).show();
        
        tv.setText(translatedText);
        
        setContentView(tv);
    }
}



Note: Download google-api-translate-java-0.95.jar from http://code.google.com/p/google-api-translate-java/downloads/list and import it as an external jar file. (Right-click project->Properties->Java Build Path-> Libraries->Add External JARS...).

35 comments:

  1. It works well,, plz tell me that how we translate from English to Urdu or from Urdu to English in Android.
    I am very thankful to you.
    Adnan Mehmood

    ReplyDelete
    Replies
    1. u bloody..how could you say that it is working???i am trying this code from last 3 days because u said "IT WORKS WELL" but it doesn't showing anything.....

      Delete
    2. well, both of you are right . google deprecated translate api in around December 11. so prior to that is was working .

      Delete
  2. The Google Translate has Urdu as a language to be translated from another one. All you have to do is to replace translatedText = t.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH); with translatedText = t.execute("Hello World", Language.ENGLISH, Language.URDU); to translate from English to Urdu. Happy coding! :)

    FYI: http://translate.google.com/#en|ur|I%20love%20you!%0A

    ReplyDelete
  3. Exception in thread "main" java.lang.Exception: Google returned the following error: [400] invalid translation language pair
    at com.google.api.translate.Translate.getJSONResponse(Translate.java:182)
    at com.google.api.translate.Translate.execute(Translate.java:71)

    ReplyDelete
  4. You must be passing the wrong parameter there!

    ReplyDelete
  5. Hi I have an exception in log cat while using Google API Translate.

    Here is the error : 12-29 09:08:36.608: I/System.out(299): Error: [google-api-translate-java] Error retrieving translation.

    How can i fix that problem?

    ReplyDelete
  6. @My World There could be 2 causes of this error: 1. Either the word or text to be translated is not correct AND/OR
    2. The language support for the desired language is not there.
    Please check.

    ReplyDelete
  7. Hi Muhammad,

    Thank you for giving me idea about this app. I'm new to android and java when I have imported this app I'm getting "cannot instantiate the type translate":

    Translate t = new Translate();

    How can I fix this problem?

    ReplyDelete
  8. I have made some changes and executing but I'm getting null value on translatedText object. Please help me with this.

    Thanks in advance.

    ReplyDelete
  9. Its not working ...
    It returns null ...

    On declaring Translate t = new Translate ();
    it adds up all the methods...
    Am i forgetting to add something ?
    Reply soon..Thank you in advance.

    ReplyDelete
  10. Google Translate API is paid API. How can we use it for development process on trial basis.

    ReplyDelete
  11. not working. An empty toast appears..

    ReplyDelete
  12. not working. An empty toast appears....how can i solve this?

    ReplyDelete
  13. it's Not working..
    showing error in
    Translate t = new Translate();
    Error is :
    "Can't instantiate the type translate"

    ReplyDelete
    Replies
    1. Hi,

      I am also getting the same error. Now If u predicated the answer for that, let me know.

      Thanks,
      Subramanian

      Delete
    2. Translate is an interface so u can not instantiate it.

      Delete
  14. The code is not working i got the error in this
    Translate t = new Translate();

    How can i fix this problem?Please give me any suggestions

    ReplyDelete
  15. hello I got this error
    java.lang.Exception: [google-api-translate-java] Referrer is not set. Call setHttpReferrer().

    ReplyDelete
    Replies
    1. same problem bro..if it worked halp me 2 brother

      Delete
  16. unfortunately this projects is closed.....why this?

    ReplyDelete
  17. google closed the translate apiv1 ..now we need to shift apiv2 ...check out that one

    ReplyDelete
  18. I am using api v2 but To translate api to charge but still I paid him some errors in translation or project falls.
    ERROR
    [google-api-translate-java] error retrieving translation

    ReplyDelete
  19. package com.example.traslater;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.SearchManager;
    import android.content.Intent;
    import android.text.Editable;
    import android.view.Menu;
    import android.view.View;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends Activity {

    String query;
    TextView t1;
    WebView w1;
    EditText e1;
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    e1=(EditText) findViewById(R.id.editText1);
    Button b1 =(Button)findViewById(R.id.button1);
    w1=(WebView)findViewById(R.id.webView1);

    // query=e1.getText().toString();
    b1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
    String url = "https://translate.google.lk/?hl=en#en/si/"+e1.getText();
    w1.setWebChromeClient(new MyWebViewClient());
    w1.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
    }});

    WebSettings webSettings = w1.getSettings();
    webSettings.setBuiltInZoomControls(true);
    webSettings.setJavaScriptEnabled(true);
    if(url != null){
    w1.loadUrl(url);
    }

    }
    });
    }


    private class MyWebViewClient extends WebChromeClient {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
    super.onProgressChanged(view, newProgress);
    }


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    }

    ReplyDelete
    Replies
    1. How to get that translated text into our android application

      Delete
  20. error at this line sir Translate t = new Translate(); tell me the sol for that

    ReplyDelete
  21. how can we import google translate api in ouer project (android studio)

    ReplyDelete
    Replies
    1. hello anum,
      put library file on your project directory "app\lib" and
      import this content in gradle file "compile files('libs/google-api-translate-java-0.95.jar')"

      Delete
    2. hello anum,
      put library file on your project directory "app\lib" and
      import this content in gradle file "compile files('libs/google-api-translate-java-0.95.jar')"

      Delete
  22. i want to use this to translate to my local language but i dnt know how.........

    ReplyDelete