Download qm file using QTranslator

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

I worked on an example of Nokia, as 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); } 
+6
file qt translation
source share
4 answers

Where are the .qm files located? Your code is trying to load a file from the current working directory, which can be anything at runtime. Specify the directory path when calling QTranslator::load :

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

Translations can be downloaded from Qt resources , so it is recommended to combine them into their executables. Then you load them something like this:

 QTranslator* translator = new QTranslator(); if (translator->load("hellotr_la", ":/resources/translations")) { app.installTranslator(translator); } 
+19
source share

The answer has already been given in a comment, but I want to clearly indicate this.

The first warning uses the 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, the problem is that the file was not found. Make sure the working directory is what you expect from it, or (better) use a resource system, as explained by andref.

+3
source share

Based on the example, you can simply try the following:

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

Hope this fixes your problem!

Note 1: There is no pointer here.
Note 2: There is no extension in your file name.

0
source share

The main steps to achieve localization in Qt are given in this link.

Hope this will be helpful to you.

-one
source share

All Articles