0

I'm calling to a Firebase method and in case of Exception I want to return the exception message in Spanish but task.getException().getMessage() is returning it in English instead.

Snippet code:

if (task.isSuccessful()) {
    // do something
} else {
    Toast.makeText(context, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}

Do I have to change something in Firebase configuration? Thanks in advance

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Patricia
  • 95
  • 9

2 Answers2

0

You can throw the Exception returned by task.getException inside a try-catch-block. All the exeptions that are thrown are in english. Below, each type of Exception that may be thrown by the method you are using.

I have uses an example from the OnCompleteListener for the createUserWithEmailAndPassword() method. Please see the follwing code:

if(!task.isSuccessful()) {
    try {
        throw task.getException();
    } catch(FirebaseAuthWeakPasswordException e) {
        //do somethig
    } catch(FirebaseAuthInvalidCredentialsException e) {
        //do somethig
    } catch(FirebaseAuthUserCollisionException e) {
        //do somethig
    } catch(Exception e) {
        Log.e("TAG", e.getMessage());
    }
}

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

getLocalizedMessage() does give you the description of the exception (the name is given by task.getException().getClass().getSimpleName()) but still in English (therefore with no difference from getMessage()). In order to make use of its "localized" features, you first need to override the method according to your needs. You can see a complete example of how to do that here, although for the simple purpose mentioned here, I would definitely stick with Alex's solution.

MGLabs
  • 91
  • 6