【问题标题】:ML Kit Firebase translate text null Kotlin AndroidML Kit Firebase 翻译文本 null Kotlin Android
【发布时间】:2020-06-24 16:28:53
【问题描述】:

我是 android 的新手,所以提前抱歉,我很喜欢在将数据发送到 firebase 数据库之前使用用户的输入并翻译它,我认为我对变量做错了,因为在循环我可以得到语言文本“en”,但在它的 null 之后,所以翻译无法开始..

     println("SAVING DATABASE")

    var theLangue:String? = null
    var detect:String? = null

    val languageIdentifier = LanguageIdentification.getClient()
    languageIdentifier.identifyPossibleLanguages(userText)
        .addOnSuccessListener { identifiedLanguages ->

            for (identifedLanguage in identifiedLanguages) {

                detect = identifedLanguage.languageTag
                val confidence = identifedLanguage.confidence
                println(" LANGUAGE DETECTED: $detect , LANGUAGE CONFIDENCE: $confidence")

            }
            theLangue = detect

        }.addOnFailureListener {

        println("problem cant translate!")
    }



    println("Translate pre build ${theLangue.toString()} ++ $theLangue")   //todo NULL
    val options = TranslatorOptions.Builder()
        .setSourceLanguage(theLangue.toString()) // todo NULL
        .setTargetLanguage(Locale.getDefault().displayLanguage)
        .build()

    val theTranslator = Translation.getClient(options)
    val conditions = DownloadConditions.Builder()
        .requireWifi()
        .build()
    theTranslator.downloadModelIfNeeded(conditions)
        .addOnSuccessListener {
            println("succes downloading models languages.. going to translate wait..")
            theTranslator.translate(userText)
                .addOnSuccessListener {
                    // Translation successful.
                    println("Succes translated text")
                }
                .addOnFailureListener { exception ->
                    // Error.
                    // ...
                    println("there is a problem failed to transalte !")
                }
        }
        .addOnFailureListener { exception ->
            // Model couldn’t be downloaded or other internal error.
            // ...
            println(exception.message)
            println(exception.localizedMessage)
            println("cant download languages models !")
        }

【问题讨论】:

    标签: android kotlin translate firebase-mlkit google-mlkit


    【解决方案1】:

    .addOnSuccessListener 是异步调用,可能不会立即执行。

    快速解决方法是将翻译逻辑移动到语言识别中的addOnSuccessListener 回调中。

    ` ... languageIdentifier.identifyPossibleLanguages(userText) .addOnSuccessListener {identifiedLanguages ->

            for (identifedLanguage in identifiedLanguages) {
    
                detect = identifedLanguage.languageTag
                val confidence = identifedLanguage.confidence
                println(" LANGUAGE DETECTED: $detect , LANGUAGE CONFIDENCE: $confidence")
    
            }
            theLangue = detect
    
    
          val options = TranslatorOptions.Builder()
            .setSourceLanguage(theLangue.toString()) // todo NULL
            .setTargetLanguage(Locale.getDefault().displayLanguage)
            .build()
          
          val theTranslator = Translation.getClient(options)
          ...
    
        }.addOnFailureListener {
    
        println("problem cant translate!")
    }
    ...
    

    `

    为了提高代码的可读性,您可以使用continueWithTask

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多