In the end I declared a dictionary in javascript that is part of the state with the translations:
var lenguajeENG = {
"hola": "hello",
...
}
By default it comes empty:
this.state = {
idioma: {}
}
And so with every language I need. After that, I have a function trad()
:
/*
* Traduce al idioma seteado la palabra pasada por parametro
*/
trad(palabra) {
if (this.state.idioma[palabra] !== undefined)
return this.state.idioma[palabra];
return palabra;
}
Every time I want to change the language all I do is change the language change the state of the variable idioma
:
/*
* Cambia el diccionario de idioma
*/
seleccionarIdioma(idioma = 'esp') {
let proximoIdioma;
switch(idioma) {
case 'eng': // Ingles
proximoIdioma = lenguajeENG;
break;
default:
idioma = 'esp'
proximoIdioma = []; // Diccionario vacio
break;
}
this.setState({ idioma: proximoIdioma });
this.idiomaSeleccionado = idioma;
};