Skip to content Skip to sidebar Skip to footer

How To Put In Yandex Translation API In An Android Application?

How do I make my android application translate from English to Hindi using Yandex translator? Java API and JSON file. I have got the API key and I've no idea what codes are to be w

Solution 1:

You can check this API: https://github.com/DoguD/Yandex-Translate-Android-API

Just import TranslatorBackgroundTask.java file to your application and then execute as shown below:

import co.oriens.yandex_translate_android_api.TranslatorBackgroundTask;
import android.util.Log;

public class MainActivity extends Activity{
//Set context
Context context=this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Default variables for translation
    String textToBeTranslated = "Hello world, yeah I know it is stereotye.";
    String languagePair = "en-fr"; //English to French ("<source_language>-<target_language>")
    //Executing the translation function
    Translate(textToBeTranslated,languagePair);
}

//Function for calling executing the Translator Background Task
void Translate(String textToBeTranslated,String languagePair){
    TranslatorBackgroundTask translatorBackgroundTask= new TranslatorBackgroundTask(context);
    String translationResult = translatorBackgroundTask.execute(textToBeTranslated,languagePair).get(); // Returns the translated text as a String
    Log.d("Translation Result",translationResult); // Logs the result in Android Monitor
}
}

For more detailed explanation you can read the README in github.


Solution 2:

1. Add dependency:

dependencies {
       ...
       implementation ('com.github.vbauer:yandex-translate-api:1.4.2'){
           exclude group: 'com.google.code.findbugs', module:'annotations'
       }
       ...
   }

   repositories{
       ...
       maven { url "https://jitpack.io" }
       ...
   }

2. In your activity or fragment:

class TranslateAsyncTask extends AsyncTask<String,Void,String>{
   @Override
   protected String doInBackground(String... strings) {
       return translate(strings[0]);
   }
   
   @Override
   protected void onPostExecute(String s) {
       //your translated text "s"
   }
}

private String translate(String text){
   YTranslateApi api = new YTranslateApiImpl(API_TRANSLATE_KEY);
   
   if(Build.VERSION.SDK_INT >= 24){
       Translation t = api.translationApi().translate(text,Language.EN);
       return t.text();
   }
   
   return "";
}

3. In Manifest.xml add:

<uses-permission android:name="android.permission.INTERNET" />

Solution 3:

You can start by checking out how to make an API call for their translate function. Their documentation in that part will show you the syntax for the HTTP request which will allow you to translate a specific piece of text and specify which languages you want to translate to and from.

In order to implement this into your Android application, you need to be able to send HTTP requests. There are many great libraries to do this. Loopj should be able to do the job. Their website will tell you how to add their library to your project/Android app.


Post a Comment for "How To Put In Yandex Translation API In An Android Application?"