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;

publicclassMainActivityextendsActivity{
//Set contextContext context=this;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

//Function for calling executing the Translator Background TaskvoidTranslate(String textToBeTranslated,String languagePair){
    TranslatorBackgroundTask translatorBackgroundTask= newTranslatorBackgroundTask(context);
    String translationResult = translatorBackgroundTask.execute(textToBeTranslated,languagePair).get(); // Returns the translated text as a StringLog.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:

classTranslateAsyncTaskextendsAsyncTask<String,Void,String>{
   @OverrideprotectedStringdoInBackground(String... strings) {
       returntranslate(strings[0]);
   }
   
   @OverrideprotectedvoidonPostExecute(String s) {
       //your translated text "s"
   }
}

privateStringtranslate(String text){
   YTranslateApi api = newYTranslateApiImpl(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-permissionandroid: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?"