A simple and free Google Translate library for Kotlin/JVM and Java.
A library that uses Google Translate and Ktor to translate text.
The discovery of an unofficial API endpoint and correct parameters are not my findings, they are from Py-Googletrans, a great library with a similar goal, but for Python.
Google Translate is one of the better translators out there, but their API is not free. This uses an unofficial API endpoint that does not require an API token.
I was looking for a free Google Translate library for Kotlin, but all I found was Py-Googletrans. This is merely a simpler Py-Googletrans, but in Kotlin.
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.therealbush:translator:1.1.0'
}
val translator = Translator()
val translation = translator.translate("Bush's translator is so cool!", Language.RUSSIAN, Language.AUTO)
println(translation.translatedText) // Переводчик Буша такой классный!
println(translation.pronunciation) // Perevodchik Busha takoy klassnyy!
println(translation.sourceLanguage) // English
-
If you are calling from a non
suspend
function, you can usetranslateBlocking(...)
-
If you want to receive a
Result<Translation>
, you can usetranslateCatching(...)
-
If you want to configure your own
HttpClient
, you can pass it toTranslator(client)
-
Operator function
Language.invoke()
can be used to resolve aLanguage
enum from a language name, code, or partial name or code:
Language.ENGLISH == Language("english") == Language("en") == Language("eng")
Translator translator = new Translator();
Translation translation = translator.translateBlocking("...", Language.Companion.INSTANCE.invoke("spanish"));
translation.getTranslatedText();
Only translateBlocking(...)
should be called from Java code. The other methods use Kotlin specific language features.
- This would not be possible without Py-Googletrans
- Ktor is a very easy to use network library
- Google Translate, but they should be cool and make their API free :)
I am not affiliated with Google, and this is using an unofficial API. This may cease to work at any point in time, and while I will try my best to keep this project up and running, you should not rely on it working all of the time.