One of my friends is going abroad – to Germany. I thought I’d make her an app that would let her enter what she wants to say in English translate it to German then speak it out.
Here goes:
I am going to be using the Bing Translate API – really cool coz they give u like two million queries, that’s a lot (unfortunately Google translate API isnt free).
First, download this java library. Place this in your libs folder.
We need the Client Id and Client username from the Microsoft MarkerPlace. Here are the instructions on how to get this.
I am gonna build this:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/etUserText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tvTranslatedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etUserText"
android:layout_centerHorizontal="true"
android:layout_marginTop="168dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/bTranslate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etUserText"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Translate" />
<Button
android:id="@+id/bSpeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTranslatedText"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="Speak" />
</RelativeLayout>
To get translated text do this:
public String translate(String text) throws Exception{
// Set the Client ID / Client Secret once per JVM. It is set statically and applies to all services
Translate.setClientId("CLIENT USERNAME");
Translate.setClientSecret("CLIENT SECRET");
String translatedText = "";
// English AUTO_DETECT -> gERMAN Change this if u wanna other languages
translatedText = Translate.execute(text,Language.GERMAN);
return translatedText;
}
The Text-To-Speech bit:
public class MainActivity extends Activity implements OnInitListener {
private TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
speakOut("Ich"); //This is how you will get the phone to speak
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.GERMAN);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
//speakOut("Ich");
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut(String text) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
And the complete code:
package com.gilo.translatr;
import java.util.Locale;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
public class MainActivity extends Activity implements OnInitListener {
private TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
((Button) findViewById(R.id.bSpeak)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
speakOut(((TextView) findViewById(R.id.tvTranslatedText)).getText().toString());
}
});
((Button) findViewById(R.id.bTranslate)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
class bgStuff extends AsyncTask<Void, Void, Void>{
String translatedText = "";
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
String text = ((EditText) findViewById(R.id.etUserText)).getText().toString();
translatedText = translate(text);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
translatedText = e.toString();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
((TextView) findViewById(R.id.tvTranslatedText)).setText(translatedText);
super.onPostExecute(result);
}
}
new bgStuff().execute();
}
});
}
public String translate(String text) throws Exception{
// Set the Client ID / Client Secret once per JVM. It is set statically and applies to all services
Translate.setClientId("CLIENT ID"); //Change this
Translate.setClientSecret("CLIENT SECRET"); //change
String translatedText = "";
translatedText = Translate.execute(text,Language.GERMAN);
return translatedText;
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.GERMAN);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
//speakOut("Ich");
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut(String text) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
There is a lot you can do, but I am feeling to lazy. Hope you learnt something.
[UPDATE] Check out the App here, I just gave it a better look but its still more or less the same thing described above.