Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
this time it's a real rename
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed May 6, 2010
1 parent 5a04844 commit d21c0c0
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
4 changes: 2 additions & 2 deletions AndroidManifest.xml
Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.translator.android" package="com.translator.android"
android:versionCode="1" android:versionCode="2"
android:versionName="1.0"> android:versionName="1.0.1">
<uses-sdk android:minSdkVersion="2" /> <uses-sdk android:minSdkVersion="2" />
<application android:label="@string/app_name" android:icon="@drawable/icon"> <application android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:name="Main" <activity android:name="Main"
Expand Down
39 changes: 39 additions & 0 deletions src/com/dmathieu/translator/Interface.java
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.translator.android;

import android.app.Activity;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.api.translate.Language;


public class Interface {

/*
* Instantiates the spinner with it's content
*/
public static ArrayAdapter getSpinnerAdapter(Activity context, Boolean has_guess) {
ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fillSpinner(adapter, has_guess);
return adapter;
}

/*
* Fills the spinner with the languages list
*/
public static void fillSpinner(ArrayAdapter adapter, Boolean has_guess) {
adapter.clear();

Integer i = 0;
for (Language l : Language.values()) {
if (i > 0 || has_guess == true) {
adapter.add(l.name());
}
i += 1;
}
}
}
52 changes: 52 additions & 0 deletions src/com/dmathieu/translator/Main.java
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.translator.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.api.translate.Language;


public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/*
* We get the two spinners : the language to translate from
* And the language to translate to. And we add them the languages list
*/
((Spinner) findViewById(R.id.languages_from)).setAdapter(Interface.getSpinnerAdapter(this, true));
((Spinner) findViewById(R.id.languages_to)).setAdapter(Interface.getSpinnerAdapter(this, false));


/*
* Whenever we click on the button, we translate the string.
*/
findViewById(R.id.translate_button).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String text = ((EditText) findViewById(R.id.text_to_translate)).getText().toString();

try {
Language lang_from = Translator.getLanguageString((Spinner) findViewById(R.id.languages_from));
Language lang_to = Translator.getLanguageString((Spinner) findViewById(R.id.languages_to));

String translatedText = Translator.translate(text, lang_from, lang_to);
((TextView) findViewById(R.id.translated_text)).setText(translatedText);
} catch(Exception e) {
/*
* It failed. We display the error message
*/
((TextView) findViewById(R.id.translated_text)).setText(e.toString());
}
}
});
}
}
32 changes: 32 additions & 0 deletions src/com/dmathieu/translator/Translator.java
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.translator.android;

import android.widget.Spinner;

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


public class Translator {
/*
* Takes the string to translate and does the work
*/
public static String translate(String text, Language lang_from, Language lang_to) throws Exception {
Translate.setHttpReferrer("http//www.dmathieu.com");
String translatedText = Translate.execute(text, lang_from, lang_to);
return translatedText;
}

/*
* Gets a language string from it's name (ENGLISH -> en)
*/
public static Language getLanguageString(Spinner spinner) throws Exception {
String lang = spinner.getSelectedItem().toString();

for (Language l : Language.values()) {
if (l.name() == lang) {
return l;
}
}
throw new Exception("Unknown language provided : " + lang);
}
}

0 comments on commit d21c0c0

Please sign in to comment.