Yandex Translate API
yandex-translate-api is a simple REST client library for Yandex.Translate. The API provides access to the Yandex online machine translation service. It supports more than 90 languages and can translate separate words or complete texts.
Online documentation: Javadoc
Prerequisites
The following actions are required to work with this library:
- Install JDK 1.8
- Get a free API key.
Setup
Gradle configuration:
repositories {
maven {
url "https://jitpack.io"
}
}
dependencies {
compile 'com.github.vbauer:yandex-translate-api:1.4.2'
}
Maven configuration:
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<dependency>
<groupId>com.github.vbauer</groupId>
<artifactId>yandex-translate-api</artifactId>
<version>1.4.2</version>
</dependency>
Usage
FYI:
- Groovy language is used in the examples just to simplify them.
- Additional documentation about the official Yandex.Translate API could be found here: https://tech.yandex.com/translate/doc/dg/concepts/api-overview-docpage/
The entry point of this library is YTranslateApi
interface (and the corresponding implementation YTranslateApiImpl
).
def key = "<your key>"
def api = new YTranslateApiImpl(key)
Using instance of this service you can work with the following features:
DetectionApi
detects the language of the specified text.LanguageApi
returns a list of translation directions supported by the service.TranslationApi
allows to translate text from one language to another.
DetectionApi
The following example returns Language.EN
:
def language = api.detectionApi().detect("Hello, World!")
LanguageApi
To retrieve all available languages without their names, use the following code snippet:
def languages = api.languageApi().all()
It is also possible to fetch names for each language, using ui
parameter:
def languages = api.languageApi().all(Language.RU)
TranslationApi
The following code should translate Russian's "Как дела?"
to some English variant ("How are you doing?"
or something similar):
def translation = api.translationApi().translate(
"Как дела?", Direction.of(Language.RU, Language.EN)
)
Source language could be also detected automatically (so you need to specify only target language):
def translation = api.translationApi().translate("Как дела?", Language.EN)
Thanks to
- Yandex LLC and all their developers for the great service.
- Vyacheslav Rusakov for the following Gradle plugins: gradle-java-lib-plugin and gradle-quality-plugin.