简体   繁体   中英

how to translate the error code from Firebase Authentication?

When I'm registering an account using Firebase sometimes error happens, I want to translate some of the error to another language. in IOS I can do something like this

Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (user, error) in


            if let error = error {

                if let errorCode = AuthErrorCode(rawValue: error._code) {

                    SVProgressHUD.dismiss()

                    switch errorCode {
                    case .networkError : self.showAlert(alertTitle: "Mohon Maaf", alertMessage: "Koneksi Internet bermasalah", actionTitle: "Kembali")
                    case .emailAlreadyInUse : self.showAlert(alertTitle: "Mohon Maaf", alertMessage: "Email yang anda masukan sudah pernah digunakan, silahkan gunakan email yang lain", actionTitle: "Kembali")
                    case .weakPassword : self.showAlert(alertTitle: "Mohon Maaf", alertMessage: "Password minimal harus memiliki 6 huruf", actionTitle: "Kembali")
                    case .invalidEmail : self.showAlert(alertTitle: "Mohon Maaf", alertMessage: "Format email yang anda masukan tidak valid, mohon diperiksa kembali.", actionTitle: "Kembali")
                    default : self.showAlert(alertTitle: "Sorry", alertMessage: "\(error.localizedDescription)", actionTitle: "Back")
                    }

                }

            }
        }

I am trying to switch the error from English to another language. I have tried but I can't find a way in Android. here is the code I use when I'm creating a user using email and password:

    FirebaseAuth.getInstance().createUserWithEmailAndPassword(email,password)
        .addOnCompleteListener { result ->

       }.addOnFailureListener { exception ->

          // want to translate the error in here

       }

java is ok.

The following line of code:

exception.getMessage();

Gets the exception message in english while the following line of code:

exception.getLocalizedMessage();

Gets a localized message. According to the official documentation regarding Throwable's getLocalizedMessage() method:

Creates a localized description of this throwable. Subclasses may override this method in order to produce a locale-specific message. For subclasses that do not override this method, the default implementation returns the same result as getMessage().

In my opinion

FirebaseAuth.getInstance().createUserWithEmailAndPassword(email,password)
    .addOnCompleteListener { result ->
      if(result.isSuccessful){
              //Do something here
      }
   }.addOnFailureListener { exception ->

      val errorCode = (e as FirebaseAuthException?)!!.errorCode
      when(errorCode){
          "ERROR_WEAK_PASSWORD" -> {
              //Do something here
          }
          else -> {
              //Do something here
          }
      }

}

If you need something more, check this link.

How to catch a Firebase Auth specific exceptions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM