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);
}
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.
精彩评论