Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase error messages in different languages?

Tags:

Showing the Firebase error messages (error.message) in the view results in english error descriptions (e.g. for Authentication errors, if user credetials contain errors).

How would you display the messages in different languages (best case: in the phone's language)?

like image 725
Jane Dawson Avatar asked Feb 24 '17 13:02

Jane Dawson


People also ask

How do I get firebase authentication error message?

Each user must have a unique email. The provided Firebase ID token is expired. The Firebase ID token has been revoked. The credential used to initialize the Admin SDK has insufficient permission to access the requested Authentication resource.

How do you handle firebase authentication exceptions?

Auth Exception Handler: As the name suggests, the AuthExceptionHandler class handles the exception by reading the error code of the firebase exception object, returns the AuthResultStatus based on the error code plus also generates an error message for every AuthResultStatus.


1 Answers

This is impossible right now. What I recommend is to use the erros code (error.code) that is a unique error code and with that you can create something to bind this errors code to your own text/language. There is an available page at Firebase documentation that have a list of those errors code that might help you with that. Check out these links: https://firebase.google.com/docs/reference/js/firebase.auth.Auth https://firebase.google.com/docs/reference/js/firebase.auth.Error https://firebase.google.com/docs/auth/admin/errors?hl=en

Edit: To solve this, I have translated it by myself (to PT-BR, my language) and implemented (in TypeScript) with these steps:

I have created an interface to hold the indexed array of string:

    export interface MessagesIndex {         [index: string]: string;     } 

Then in some UI or Error Service, I've declared this variable as the Interface above:

    params = {             'invalid-argument': 'Erro: Um argumento inválido foi fornecido.',             'invalid-disabled-field': 'Erro: O valor fornecido para a                propriedade de usuário é inválido.',               /* ADD HERE THE OTHERs IDs AND THE CORRESPONDING MESSAGEs */          } as MessagesIndex; 

After that, I've created a function to print it by the given code (from Firebase), remember to split because the error.code atribute comes like "auth/error-id" and what we only need here is the "error-id", and if the error code is not found, then you can return some "Unknown error" and print the error.code, if you want:

    public printErrorByCode(code: string): string {          code = code.split('/')[1];          if (this.params[code]) {              return (this.params[code]);          } else {              return ('Ocorreu algum erro desconhecido! \n Codigo erro: ' + code);          }      }  

It's not the best code but I hope it helps!

like image 172
Frederiko Cesar Avatar answered Oct 05 '22 04:10

Frederiko Cesar