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...).
<?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...).
It works well,, plz tell me that how we translate from English to Urdu or from Urdu to English in Android.
ReplyDeleteI am very thankful to you.
Adnan Mehmood
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.....
Deletewell, both of you are right . google deprecated translate api in around December 11. so prior to that is was working .
DeleteThe 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! :)
ReplyDeleteFYI: http://translate.google.com/#en|ur|I%20love%20you!%0A
Exception in thread "main" java.lang.Exception: Google returned the following error: [400] invalid translation language pair
ReplyDeleteat com.google.api.translate.Translate.getJSONResponse(Translate.java:182)
at com.google.api.translate.Translate.execute(Translate.java:71)
You must be passing the wrong parameter there!
ReplyDeleteHi I have an exception in log cat while using Google API Translate.
ReplyDeleteHere 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?
@My World There could be 2 causes of this error: 1. Either the word or text to be translated is not correct AND/OR
ReplyDelete2. The language support for the desired language is not there.
Please check.
Hi Muhammad,
ReplyDeleteThank 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?
I have made some changes and executing but I'm getting null value on translatedText object. Please help me with this.
ReplyDeleteThanks in advance.
Its not working ...
ReplyDeleteIt 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.
Google Translate API is paid API. How can we use it for development process on trial basis.
ReplyDeletent working
ReplyDeletenot working. An empty toast appears..
ReplyDeletenot working. An empty toast appears....how can i solve this?
ReplyDeleteit's Not working..
ReplyDeleteshowing error in
Translate t = new Translate();
Error is :
"Can't instantiate the type translate"
The code is not working i got the error in this
ReplyDeleteTranslate t = new Translate();
How can i fix this problem?Please give me any suggestions
hello I got this error
ReplyDeletejava.lang.Exception: [google-api-translate-java] Referrer is not set. Call setHttpReferrer().
unfortunately this projects is closed.....why this?
ReplyDeletemeans forcefully closed error
ReplyDeletegoogle closed the translate apiv1 ..now we need to shift apiv2 ...check out that one
ReplyDelete