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);
}

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);
}

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.


Based on the example, can you simply try this :

 QTranslator translator;
 translator.load("hellotr_la");
 app.installTranslator(&translator);

Hope it will fix your problem !

Note 1 : No pointer here. Note 2 : No extension in your filename.


Need Your Help

Page_ClientValidate() with multiple ValidationGroups - how to show multiple summaries simultaneously?

asp.net validation validationsummary

ASP.NET 2.0. Lets say i have two Validation Groups valGrpOne and valGrpTwo; and two Validation Summaries valSummOne and valSummTwo; Reason for breaking up sections is purely aesthetic. One submit b...

There's no @Html.Button !

.net asp.net-mvc-3 razor html-helper htmlbutton

this is weird. I see references out there for @Html.Button() but when I type that Intellisense doesn't find such a helper... there's dropdownlist, hidden, editors, et cetera, but no button!