I'm still a newby in Kotlin and Android development, and I wanted to translate some text (ingredients from a product) to Spanish given a language code.
Code (forget Spanish error messages) from a function:
private fun translateTextToSpanish(ingredients: String, it: String?): String {
val opcionesTraductor: TranslatorOptions
if(it == "en")
opcionesTraductor = TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(TranslateLanguage.SPANISH)
.build()
else
if(it == "fr")
opcionesTraductor = TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.FRENCH)
.setTargetLanguage(TranslateLanguage.SPANISH)
.build()
else
if(it == "de")
opcionesTraductor = TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.GERMAN)
.setTargetLanguage(TranslateLanguage.SPANISH)
.build()
else
if(it == "it")
opcionesTraductor = TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ITALIAN)
.setTargetLanguage(TranslateLanguage.SPANISH)
.build()
else
if(it == "pt")
opcionesTraductor = TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.PORTUGUESE)
.setTargetLanguage(TranslateLanguage.SPANISH)
.build()
else
if(it == "ru")
opcionesTraductor = TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.RUSSIAN)
.setTargetLanguage(TranslateLanguage.SPANISH)
.build()
else
opcionesTraductor = TranslatorOptions.Builder()
.setSourceLanguage(TranslateLanguage.ENGLISH)
.setTargetLanguage(TranslateLanguage.SPANISH)
.build()
val traductorFinal = Translation.getClient(opcionesTraductor)
var traduccion = ""
val conditions = DownloadConditions.Builder()
.requireWifi()
.build()
traductorFinal.downloadModelIfNeeded(conditions)
.addOnSuccessListener {
traductorFinal.translate(ingredients)
.addOnSuccessListener { translatedText ->
traduccion = translatedText
}
.addOnFailureListener {
val dialog = AlertDialog.Builder(this)
dialog.setTitle("馃棧 Error en la traducci贸n")
dialog.setMessage("Se ha producido un error en la traducci贸n.\n\nVuelve a intentarlo m谩s tarde, y si el problema persiste, contacta con nosotros.")
dialog.setNegativeButton("Cerrar", null)
dialog.show()
}
}
.addOnFailureListener {
val dialog = AlertDialog.Builder(this)
dialog.setTitle("馃棧 Error en la traducci贸n")
dialog.setMessage("Se ha producido un error en la traducci贸n.\n\nVuelve a intentarlo m谩s tarde, y si el problema persiste, contacta con nosotros.")
dialog.setNegativeButton("Cerrar", null)
dialog.show()
}
return traduccion
}
This, as it is, returns "", which after analyzing the code, I can't figure out what's missing here...
I'm using the latest ML Kit API.
Thanks!
Solution:
private fun translateTextToSpanish(ingredients: String, it: String, callback: (String) -> Unit) {
val sourceLanguage = TranslateLanguage.fromLanguageTag(it) ?: TranslateLanguage.ENGLISH
val targetLanguage = TranslateLanguage.SPANISH
val options = TranslatorOptions.Builder()
.setSourceLanguage(sourceLanguage)
.setTargetLanguage(targetLanguage)
.build()
val translator = Translation.getClient(options)
val conditions = DownloadConditions.Builder().requireWifi().build()
translator.downloadModelIfNeeded(conditions).addOnSuccessListener {
translator.translate(ingredients).addOnSuccessListener { translatedText ->
callback(translatedText)
}.addOnFailureListener { exception ->
val dialog = AlertDialog.Builder(this)
dialog.setTitle("馃棧 Error en la traducci贸n")
dialog.setMessage("Error: ${exception.message}")
dialog.setNegativeButton("Cerrar", null)
dialog.show()
}
}.addOnFailureListener { exception ->
val dialog = AlertDialog.Builder(this)
dialog.setTitle("馃棧 Error en la traducci贸n")
dialog.setMessage("Error: ${exception.message}")
dialog.setNegativeButton("Cerrar", null)
dialog.show()
}
}
Using callback() and lambda worked without issues.