Multi-Language Translate

Extends from previous exercise, AndroidTranslate, using Google Translate API in Android application. It's a multi-language translation application. There are two spinner in the application, user can select the language to be translated.



------------------------------------------
Please note:
- google-api-translate-java have to be downloaded and build path have to be set, refer the article google-api-translate-java.
- "android.permission.INTERNET" have to be set in AndroidMainfest.xml, refer to the article AndroidTranslate, using Google Translate API in Android application.
------------------------------------------

Modify main.xml to add two Spinner to select the language to be translated from and to.

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:id="@+id/InputText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="from:"
/>
android:id = "@+id/spinner_InputLanguage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="to:"
/>
android:id = "@+id/spinner_OutputLanguage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:id="@+id/TranslateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Translate"
/>
android:id="@+id/OutputText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>


main.xml can be downloaded here.

Implement a file arrayLanguage.xml in the folder /res/values/




ARABIC
BULGARIAN
CATALAN
CHINESE
CHINESE_SIMPLIFIED
CHINESE_TRADITIONAL
CROATIAN
CZECH
DANISH
DUTCH
ENGLISH
FILIPINO
FINNISH
FRENCH
GERMAN
GREEK
HEBREW
HINDI
INDONESIAN
ITALIAN
JAPANESE
KOREAN
LATVIAN
LITHUANIAN
NORWEGIAN
POLISH
PORTUGESE
ROMANIAN
RUSSIAN
SERBIAN
SLOVAK
SLOVENIAN
SPANISH
SWEDISH
UKRANIAN
VIETNAMESE



arrayLanguage.xml can be downloaded here.

Modify AndroidTranslate.java
package com.exercise.AndroidTranslate;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

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



public class AndroidTranslate extends Activity {

EditText MyInputText;
Button MyTranslateButton;
TextView MyOutputText;
Spinner spinner_InputLanguage, spinner_OutputLanguage;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

MyInputText = (EditText)findViewById(R.id.InputText);
MyTranslateButton = (Button)findViewById(R.id.TranslateButton);
MyOutputText = (TextView)findViewById(R.id.OutputText);

MyTranslateButton.setOnClickListener(MyTranslateButtonOnClickListener);

ArrayAdapter adapter
= ArrayAdapter.createFromResource(this,
R.array.language, android.R.layout.simple_spinner_item);

spinner_InputLanguage = (Spinner) findViewById(R.id.spinner_InputLanguage);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_InputLanguage.setAdapter(adapter);

spinner_OutputLanguage = (Spinner) findViewById(R.id.spinner_OutputLanguage);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_OutputLanguage.setAdapter(adapter);
}

private Button.OnClickListener MyTranslateButtonOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String InputString;
String OutputString = null;
InputString = MyInputText.getText().toString();

Language fromLanguage =
Language.valueOf((String)spinner_InputLanguage
.getItemAtPosition((int) spinner_InputLanguage
.getSelectedItemId()));
Language toLanguage =
Language.valueOf((String)spinner_OutputLanguage
.getItemAtPosition((int) spinner_OutputLanguage
.getSelectedItemId()));

try {
Translate.setHttpReferrer("http://picturedmagazine.blogspot.com/");
OutputString = Translate.execute(InputString,
fromLanguage, toLanguage);

} catch (Exception ex) {
ex.printStackTrace();
OutputString = "Error";
}

MyOutputText.setText(OutputString);
}
};
}

AndroidTranslate.java can be downloaded here.

In this exercise, two spinner are used to select the language to be translated from and to. A array-string are used to store the languages.

It can be retrieved using:
(String)spinner_InputLanguage.getItemAtPosition((int) spinner_InputLanguage.getSelectedItemId())
(String)spinner_OutputLanguage.getItemAtPosition((int) spinner_OutputLanguage.getSelectedItemId())

It's same as the parameters to be passed to Translate.execute(), but in String type, not the expected Language type. So we have to use the Language.valueOf() to convert it to Language type.

Post a Comment

Previous Post Next Post

Contact Form