How to put in Yandex translation API in an android

2019-09-06 21:36发布

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 written to include the API and make it work. It will be really helpful if you post the entire code :D Thanks.

2条回答
太酷不给撩
2楼-- · 2019-09-06 21:48

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-09-06 21:48

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.

查看更多
登录 后发表回答