This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |||
} | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |||
} | |||
} | |||
}); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |||
} | |||
} |