Issue
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.
Solution
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().
Answered By - Alex Mamo
Answer Checked By - Gilberto Lyons (JavaFixing Admin)