Translate React JS without Node

0

The question is simple, there is some way to translate my site to another language (from Spanish to Portuguese for example) as it could be done with this library but without installing Node and migrating the entire App made to it?

In case you can not, what alternatives do I have left?

Thank you very much!

    
asked by Genarito 14.02.2018 в 20:45
source

1 answer

0

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;
};
    
answered by 01.04.2018 / 21:52
source