Simple util which helps to implement Yandex translation service with RxJava. Example of using:
Add to build.gradle file:
dependencies {
compile 'io.reactivex:rxjava:1.1.6'
compile 'io.reactivex:rxandroid:1.2.1'
}
String sourceText = "Годный переводчик";
String sourceLang = "ru";
String destinationLang = "en";
String key = "your_key";
YandexTranslation translation = new YandexTranslation();
translation.setKey(key)
.getTextObservable(sourceText, sourceLang, destinationLang)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String text) {
}
});
translation.setKey(key)
.getTranslationObservable(sourceText, sourceLang, destinationLang)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<YandexTranslation.Translation>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(YandexTranslation.Translation t) {
}
});
Translation translation = getTranslation(sourceText, sourceLang, destinationLang);
if (YandexTranslation.Translation.hasTranslation(translation)){
String text = translation.translations.get(0);
}