Introduction

Today we will make a language translator. Language translators are very important, especially for people who travel abroad. This app allows users to speak any language the user wants without even knowing that language.
In this app, the user will enter the text that he/she wants to convert. The user will get the translated text on a button click. The user will not only be able to read but hear the translated text.
Step 1
We will be using Microsoft Translator for this. Prior to making this app, you will need to register yourself in the "Microsoft Marketplace". For registering, visit http://blogs.msdn.com/b/translation/p/gettingstarted1.aspx. We will be using the "Client ID" and "Client Secret" provided in this app.
Step 2
Download the "microsoft-translator-java-api-0.6.1-jar-with-dependenciesjar" file provided in "library.rar" above.
Include this jar as a library in the Eclipse project for your language translator.
Step 3
"string.xml" used in this app is:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">LangTranslatoeFinal</string>
  4. <string name="action_settings">Settings</string>
  5. <string name="hello_world">Hello world!</string>
  6. <string name="choose_lang">Choose Language</string>
  7. <string-array name="lang">
  8. <item>ENGLISH</item>
  9. <item>FRENCH</item>
  10. <item>GERMAN</item>
  11. <item>ITALIAN</item>
  12. </string-array>
  13. </resources>
Step 4
Open "activity_main" and add the following code to it:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#ffd088">
  6. <EditText
  7. android:id="@+id/text"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentTop="true"
  11. android:layout_centerHorizontal="true"
  12. android:layout_marginTop="31dp"
  13. android:ems="10" >
  14. <requestFocus />
  15. </EditText>
  16. <LinearLayout
  17. android:layout_height="wrap_content"
  18. android:layout_width="fill_parent"
  19. android:orientation="vertical" >
  20. <TextView
  21. android:id="@+id/translatedtext"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_below="@+id/etUserText"
  25. android:layout_centerHorizontal="true"
  26. android:layout_marginTop="168dp"
  27. android:text=""
  28. android:layout_marginLeft="90dp"
  29. android:textAppearance="?android:attr/textAppearanceLarge" />
  30. <Spinner
  31. android:id="@+id/selectLanguage"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_below="@+id/etUserText"
  35. android:layout_centerHorizontal="true"
  36. android:layout_marginTop="50dp"
  37. android:prompt="@string/choose_lang"
  38. android:entries="@array/lang"
  39. android:layout_marginLeft="90dp"/>
  40. <Button
  41. android:id="@+id/say"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:layout_below="@+id/tvTranslatedText"
  45. android:layout_centerHorizontal="true"
  46. android:layout_marginTop="46dp"
  47. android:text="Say"
  48. android:background="@drawable/button_lay"
  49. android:layout_marginLeft="100dp" />
  50. </LinearLayout>
  51. </RelativeLayout>
EditText will take the text from the user that needs to be converted.
TextView will display the translated text.
Spinner is for the user to choose the language in which he/she wants to translate the entered text.
Button click will display the translated text and voice.
The layout looks like:
im1.jpg
Step 5
Open "MainActivity" and add the following code to it:
  1. package com.chhavi.langtranslatoefinal;
  2. import java.util.Locale;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.speech.tts.TextToSpeech;
  8. import android.util.Log;
  9. import android.view.Menu;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.Spinner;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17. import com.memetix.mst.language.Language;
  18. import com.memetix.mst.translate.Translate;
  19. public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
  20. private int check_code = 0;
  21. Spinner lang;
  22. Button say;
  23. EditText text;
  24. TextView translatedText;
  25. String original=null;
  26. String translated=null;
  27. String langSelected;
  28. private TextToSpeech convert;
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33. lang=(Spinner)findViewById(R.id.selectLanguage);
  34. say=(Button)findViewById(R.id.say);
  35. text=(EditText)findViewById(R.id.text);
  36. translatedText=(TextView)findViewById(R.id.translatedtext);
  37. Intent check = new Intent();
  38. check.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
  39. startActivityForResult(check, check_code);
  40. say.setOnClickListener(new OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. class bgStuff extends AsyncTask<Void, Void, Void>{
  44. @Override
  45. protected Void doInBackground(Void... params) {
  46. // TODO Auto-generated method stub
  47. Translate.setClientId("CLIENT ID"); //Change this
  48. Translate.setClientSecret("CLIENT SECRET"); //Change this
  49. Log.i("calling....................","speak");
  50. original=text.getText().toString();
  51. Log.i("Original Text is.............",original);
  52. langSelected=String.valueOf(lang.getSelectedItem());
  53. Log.i("Language selected.........",langSelected);
  54. if (text!=null && text.length()>0) {
  55. try
  56. {
  57. if(langSelected.equalsIgnoreCase("ENGLISH"))
  58. {
  59. convert.setLanguage(Locale.US);
  60. translated = Translate.execute(original, Language.ENGLISH);
  61. Log.i("translated text................", translated);
  62. }
  63. else if(langSelected.equalsIgnoreCase("GERMAN"))
  64. {
  65. convert.setLanguage(Locale.GERMAN);
  66. translated = Translate.execute(original, Language.GERMAN);
  67. Log.i("translated text................", translated);
  68. }
  69. else if(langSelected.equalsIgnoreCase("FRENCH"))
  70. {
  71. convert.setLanguage(Locale.FRENCH);
  72. translated = Translate.execute(original, Language.FRENCH);
  73. Log.i("translated text................", translated);
  74. }
  75. else if(langSelected.equalsIgnoreCase("ITALIAN"))
  76. {
  77. convert.setLanguage(Locale.ITALIAN);
  78. translated = Translate.execute(original, Language.ITALIAN);
  79. Log.i("translated text................", translated);
  80. }
  81. }
  82. catch(Exception e)
  83. {
  84. Log.i("Error in translation.........",e.toString());
  85. }
  86. }
  87. return null;
  88. }
  89. @Override
  90. protected void onPostExecute(Void result) {
  91. translatedText.setText(translated);
  92. Toast.makeText(MainActivity.this, "Saying: " + translated, Toast.LENGTH_LONG).show();
  93. convert.speak(translated, TextToSpeech.QUEUE_ADD, null);
  94. }
  95. }
  96. new bgStuff().execute();
  97. }
  98. });
  99. }
  100. @Override
  101. public boolean onCreateOptionsMenu(Menu menu) {
  102. // Inflate the menu; this adds items to the action bar if it is present.
  103. getMenuInflater().inflate(R.menu.main, menu);
  104. return true;
  105. }
  106. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  107. if (requestCode == check_code) {
  108. if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
  109. // success, create the TTS instance
  110. convert = new TextToSpeech(this, this);
  111. }
  112. else {
  113. // missing data, install it
  114. Intent installIntent = new Intent();
  115. installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
  116. startActivity(installIntent);
  117. }
  118. }
  119. }
  120. @Override
  121. public void onInit(int status) {
  122. if (status == TextToSpeech.SUCCESS) {
  123. Toast.makeText(MainActivity.this,"Engine is initialized", Toast.LENGTH_LONG).show();
  124. int result = convert.setLanguage(Locale.GERMAN);
  125. if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
  126. Log.i("TTS", "This Language is not supported");
  127. }
  128. else {
  129. //speakOut("Ich");
  130. Log.i("TTS", "This Language is supported");
  131. }
  132. }
  133. else if (status == TextToSpeech.ERROR) {
  134. Toast.makeText(MainActivity.this,"Error occurred while initializing engine", Toast.LENGTH_LONG).show();
  135. }
  136. }
  137. }
Text-To-Speech (TTS), also known as "speech synthesis", enables your Android device to "speak" text of various languages. The TextToSpeech engine supports many languages, like English, Spanish, German, Italian and so on.
In the code above, convert is an object of TextToSpeech.
The "convert.setLanguage" function will allow you to produce the voice in the accent of the specified country. Some languages are spoken with a different accent in different countries. You will clearly be able to see what difference it makes if you use this without the Translate.execute function.
The "Translate.execute" function performs language translation.
The "convert.speak" function will produce the voice.
Note that the code above will not work until and unless you give your "CLIENT ID" and "CLIENT SECRET" provided to you during registration, in "Translate.setClientId" and "Translate.setClientSecret" respectively.
Step 6
Add the internet permission in "AndroidManifest.xml" as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.chhavi.langtranslatoefinal"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-permission android:name="android.permission.INTERNET"/>
  7. <uses-sdk
  8. android:minSdkVersion="8"
  9. android:targetSdkVersion="17" />
  10. <application
  11. android:allowBackup="true"
  12. android:icon="@drawable/ic_launcher"
  13. android:label="@string/app_name"
  14. android:theme="@style/AppTheme" >
  15. <activity
  16. android:name="com.chhavi.langtranslatoefinal.MainActivity"
  17. android:label="@string/app_name" >
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN" />
  20. <category android:name="android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. </activity>
  23. </application>
  24. </manifest>
Output snapshots:
Run this application on an Android phone.
im2f.png
Enter the text.
im3f.png
Clicking on the Spinner will give you the following language list:
im4f.png
Choosing "German" from the spinner and clicking on the "Say" button will give you (note that you will hear the translated text at this time):
im5f.png
Translating into "Italian":
im6f.png
Translating into "French":
im7f.png
Similarly, you can type text in any of these languages and get the translated text in the required language.

Summary

In this article, you learned two things: how to convert text to speech (accent taken care of) and how to translate text into some other language.
Thank you....... Enjoy coding :)