├── LICENSE ├── README.md └── co └── oriens └── yandex_translate_android_api └── TranslatorBackgroundTask.java /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Doğu Deniz Uğur 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Yandex Translate Android API 2 | A background task, which lets you use Yandex.Translate API in Android projects easily. 3 | ## Requires 4 | 5 | * A Yandex API Key - [Sign Up Here](http://api.yandex.com/translate/) 6 | 7 | 8 | Quickstart 9 | ========== 10 | - In TranslatorBackgroundTask.java put your API key in `String yandexKey = "YOUR_API_KEY";` 11 | - Below is a MainActivity.java file which executes the Yandex Translate Background Task 12 | ```java 13 | import co.oriens.yandex_translate_android_api.TranslatorBackgroundTask; 14 | import android.util.Log; 15 | 16 | public class MainActivity extends Activity{ 17 | //Set context 18 | Context context=this; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_main); 24 | 25 | //Default variables for translation 26 | String textToBeTranslated = "Hello world, yeah I know it is stereotye."; 27 | String languagePair = "en-fr"; //English to French ("-") 28 | //Executing the translation function 29 | Translate(textToBeTranslated,languagePair); 30 | } 31 | 32 | //Function for calling executing the Translator Background Task 33 | void Translate(String textToBeTranslated,String languagePair){ 34 | TranslatorBackgroundTask translatorBackgroundTask= new TranslatorBackgroundTask(context); 35 | String translationResult = translatorBackgroundTask.execute(textToBeTranslated,languagePair); // Returns the translated text as a String 36 | Log.d("Translation Result",translationResult); // Logs the result in Android Monitor 37 | } 38 | } 39 | 40 | ``` 41 | -------------------------------------------------------------------------------- /co/oriens/yandex_translate_android_api/TranslatorBackgroundTask.java: -------------------------------------------------------------------------------- 1 | package co.oriens.yandex_translate_android_api; 2 | 3 | import android.content.Context; 4 | import android.os.AsyncTask; 5 | import android.util.Log; 6 | 7 | import java.io.BufferedReader; 8 | import java.io.IOException; 9 | import java.io.InputStream; 10 | import java.io.InputStreamReader; 11 | import java.net.HttpURLConnection; 12 | import java.net.MalformedURLException; 13 | import java.net.URL; 14 | 15 | /** 16 | * Created by DoguD on 01/07/2017. 17 | */ 18 | 19 | public class TranslatorBackgroundTask extends AsyncTask { 20 | //Declare Context 21 | Context ctx; 22 | //Set Context 23 | TranslatorBackgroundTask(Context ctx){ 24 | this.ctx = ctx; 25 | } 26 | 27 | @Override 28 | protected String doInBackground(String... params) { 29 | //String variables 30 | String textToBeTranslated = params[0]; 31 | String languagePair = params[1]; 32 | 33 | String jsonString; 34 | 35 | try { 36 | //Set up the translation call URL 37 | String yandexKey = "YOUR_API_KEY"; 38 | String yandexUrl = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=" + yandexKey 39 | + "&text=" + textToBeTranslated + "&lang=" + languagePair; 40 | URL yandexTranslateURL = new URL(yandexUrl); 41 | 42 | //Set Http Conncection, Input Stream, and Buffered Reader 43 | HttpURLConnection httpJsonConnection = (HttpURLConnection) yandexTranslateURL.openConnection(); 44 | InputStream inputStream = httpJsonConnection.getInputStream(); 45 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 46 | 47 | //Set string builder and insert retrieved JSON result into it 48 | StringBuilder jsonStringBuilder = new StringBuilder(); 49 | while ((jsonString = bufferedReader.readLine()) != null) { 50 | jsonStringBuilder.append(jsonString + "\n"); 51 | } 52 | 53 | //Close and disconnect 54 | bufferedReader.close(); 55 | inputStream.close(); 56 | httpJsonConnection.disconnect(); 57 | 58 | //Making result human readable 59 | String resultString = jsonStringBuilder.toString().trim(); 60 | //Getting the characters between [ and ] 61 | resultString = resultString.substring(resultString.indexOf('[')+1); 62 | resultString = resultString.substring(0,resultString.indexOf("]")); 63 | //Getting the characters between " and " 64 | resultString = resultString.substring(resultString.indexOf("\"")+1); 65 | resultString = resultString.substring(0,resultString.indexOf("\"")); 66 | 67 | Log.d("Translation Result:", resultString); 68 | return jsonStringBuilder.toString().trim(); 69 | 70 | } catch (MalformedURLException e) { 71 | e.printStackTrace(); 72 | } catch (IOException e) { 73 | e.printStackTrace(); 74 | } 75 | return null; 76 | } 77 | 78 | @Override 79 | protected void onPreExecute() { 80 | super.onPreExecute(); 81 | } 82 | 83 | @Override 84 | protected void onPostExecute(String result) { 85 | } 86 | 87 | @Override 88 | protected void onProgressUpdate(Void... values) { 89 | super.onProgressUpdate(values); 90 | } 91 | } 92 | --------------------------------------------------------------------------------