Doubt with QTranslator

0

I create a small test interface consisting of two buttons, a table and a combobox to change the language. All without Designer.

botonRedo = new QPushButton(QIcon("../Prueba/iconos/rehacer.png"),tr("Rehacer"));
botonUndo = new QPushButton(QIcon("../Prueba/iconos/deshacer.png"),tr("Deshacer"));

comboLenguajes= new QComboBox;
QStringList listaIdiomas;
listaIdiomas<<tr("Español")<<tr("Ingles");
comboLenguajes->addItems(listaIdiomas);

Object::connect(comboLenguajes,SIGNAL(currentIndexChanged(int)),this,SLOT(CambiarIdioma(int)));

The test widget has a table with this header:

cabecera<<tr("Amigo")<<tr("Ciudad")<<tr("Color")<<tr("Sexo");

The function (slot) that the combobox calls is this:

void Widget::CambiarIdioma(int indice)
{
    QString filename = "/home/Otras/programacion/Qt/MVC/Prueba/Prueba/prueba_en";
    if (indice==1)
    {        
        listaTranslator.load(filename);
        qApp->installTranslator(&listaTranslator);
    }
    else
    {
        qApp->removeTranslator(&listaTranslator);
    }
}

And finally I have created this event:

void Widget::changeEvent(QEvent* event)
{
    if (event->type() == QEvent::LanguageChange)
    {
        CambiarTraduccion();
    }
    QWidget::changeEvent(event);
}

Calling this function:

void Widget::CambiarTraduccion()
{
    botonRedo->setText(tr("redo"));
    botonUndo->setText(tr("undo"));
    QStringList nombresIngles;
    nombresIngles<<tr("Name")<<tr("City")<<tr("Colour")<<tr("Sex");
    modelo->TraducirCabecera(nombresIngles);

}

The translation is created on the other hand with QLinguist, which has created the corresponding * .qm file.

The fact is that when I change the language first (Spanish) through e combobox, the thing works and everything happens in English, but when I put it back in Spanish it does not work. I also believe that I must have some conceptual problem, because I do not know why I have to translate and include the translation file, when I then have to create an ad hoc function to include the translations. Something is missing me.

    
asked by user3733164 25.09.2017 в 11:05
source

1 answer

0

The translation does not work as you plan and proof of this is that you can eliminate all calls to tr and the example will work exactly the same.

When you put a string such that:

QObject::tr("algo");

Qt will use that string as an identifier when searching for translations.

Once you have modified the application so that all the translatable strings make use of tr , it is to open QtLinguist and create a language file for each translation you want to apply ( if you want to return to the original texts in execution time you will have to create a translation file for them).

To add the translation files to the project you have to edit the project file:

TRANSLATIONS = languages/translation_en.ts  languages/translation_es.ts

And now, to change the translation file used at every moment you have to use removeTranslator e installTranslator

QTranslator translator;

// eliminar la traduccion antigua
qApp->removeTranslator(&translator);

// cargar la nueva
qApp->installTranslator(&translator);

And with this the translation of the texts will be done automatically.

You have a tutorial on this subject at the following link

    
answered by 25.09.2017 в 11:22