Skip to content
Browse files

this time it's a real rename

  • Loading branch information...
1 parent 5a04844 commit d21c0c03ed128a9eb7d448594a87b94b82d86e11 @dmathieu committed
View
4 AndroidManifest.xml
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.translator.android"
- android:versionCode="1"
- android:versionName="1.0">
+ android:versionCode="2"
+ android:versionName="1.0.1">
<uses-sdk android:minSdkVersion="2" />
<application android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:name="Main"
View
39 src/com/dmathieu/translator/Interface.java
@@ -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;
+ }
+ }
+}
View
52 src/com/dmathieu/translator/Main.java
@@ -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());
+ }
+ }
+ });
+ }
+}
View
32 src/com/dmathieu/translator/Translator.java
@@ -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.
Something went wrong with that request. Please try again.