Home > Enterprise >  Why is the Translation client not returning translated text?
Why is the Translation client not returning translated text?

Time:12-26

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("           
  • Related