Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loading qm file using QTranslator

I'm trying to use translation files. I went through all the procedures: created ts file, translated it, but when I run the application, the language is still the same as before.

I worked on the Nokia example, just like in the instructions.

What could be my problem?

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QTranslator* translator=new QTranslator(0);

    if(QFile::exists("hellotr_la.qm"))
        qWarning("failed-no file");

    if(! translator->load("hellotr_la.qm"))
        qWarning("failed loading"); //the warning appears ****

    app.installTranslator(translator);
}
like image 299
sara Avatar asked Oct 27 '10 14:10

sara


2 Answers

Where are the .qm files located? Your code is attempting to load the file from the current working directory, which can be anything during runtime. Specify a directory path in the call to QTranslator::load:

QTranslator* translator = new QTranslator();
if (translator->load("hellotr_la", "/path/to/folder/with/qm/files")) {
    app.installTranslator(translator);
}

Translations can be loaded from Qt resources, so it is a good idea to bundle them inside your executables. Then you would load them somewhat like this:

QTranslator* translator = new QTranslator();
if (translator->load("hellotr_la", ":/resources/translations")) {
    app.installTranslator(translator);
}
like image 146
andref Avatar answered Nov 20 '22 20:11

andref


The answer was already given in a comment, but I want to point it out clearly.

The first warning uses a wrong condition:

if(QFile::exists("hellotr_la.qm"))
    qWarning("failed-no file");

It should be:

if(!QFile::exists("hellotr_la.qm"))
    qWarning("failed-no file");

Since you only saw the second warning, but not the first one, the problem is indeed that the file was not found. Make sure that the working directory is what you expect it to be or (better) use the resource system as explained by andref.

like image 35
Sebastian Negraszus Avatar answered Nov 20 '22 19:11

Sebastian Negraszus