I recently implemented the MS Cognitive Services Translate API for Wordee. I switched to it after trying Google Translate API and I did that for two reasons:
The application I uploaded to illustrate the example consists of a Spinner to select the target language, an EditText to enter the text you want to translate, a Button to perform the translation request and a TextView to show the translated text. The API automatically detects the source language.
This example is fully functional, you only need to provide a valid API key.
There are also two important things to clarify in this example. First, we saved the languages in a JSON file, that we have to retrieve and parse in order to populate the Spinner. Second, we have to perform the translation using an Async task, or we'll get a NetworkOnMainThreadException.
Do not forget to protect your API key and, of course, never upload it to public repositories.
Public repository on Github.
- Microsoft's API is better documented.
- Google's API costs twice the MS's API.
The application I uploaded to illustrate the example consists of a Spinner to select the target language, an EditText to enter the text you want to translate, a Button to perform the translation request and a TextView to show the translated text. The API automatically detects the source language.
This example is fully functional, you only need to provide a valid API key.
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
public class MStranslate { | |
private final static String API_KEY = "YOUR_API_KEY_HERE"; | |
private final static String TRANS_URL = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to="; | |
private static final String TAG = MStranslate.class.getName(); | |
There are also two important things to clarify in this example. First, we saved the languages in a JSON file, that we have to retrieve and parse in order to populate the Spinner. Second, we have to perform the translation using an Async task, or we'll get a NetworkOnMainThreadException.
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
private class TransAsynTask extends AsyncTask<String, Void, String>{ | |
@Override | |
protected String doInBackground(String... strings) { | |
MStranslate mStranslate = new MStranslate(); | |
String translationResult = mStranslate.performTrans(strings[0], strings[1]); | |
return translationResult; | |
} | |
@Override | |
protected void onPostExecute(String s) { | |
transResult.setText("Translation: " + s); | |
super.onPostExecute(s); | |
} | |
} |
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
private void loadJSON() { | |
// get json resource file | |
InputStream is = getResources().openRawResource(R.raw.languages_list); | |
Scanner scanner = new Scanner(is); | |
StringBuilder builder = new StringBuilder(); | |
while(scanner.hasNextLine()) { | |
builder.append(scanner.nextLine()); | |
} | |
parseJson(builder.toString()); | |
} | |
private void parseJson(String s) { | |
try { | |
Gson gson = new Gson(); | |
languages = gson.fromJson(s,Map.class); | |
// initialize spinner arraylist | |
ArrayList<String> spinnerArray = new ArrayList<>(); | |
for (Object key : languages.keySet()) { | |
// add each language to spinner's arraylist | |
spinnerArray.add(key.toString()); | |
} | |
// set up spinner | |
spinnerAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, spinnerArray); | |
spinner.setAdapter(spinnerAdapter); | |
} catch (Exception e) { | |
Log.e(TAG, e.getMessage()); | |
} |
Public repository on Github.
Comentarios
Publicar un comentario