Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTranslator doesn't work

Tags:

qt

#include<QApplication>
#include<QTranslator>
#include<QObject>
#include<QTextCodec>
#include<QWidget>

int main(int argc, char* argv[])
{
    QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
    QApplication app(argc, argv);

    QTranslator translator;
    translator.load("app_zh_CN.qm");
    app.installTranslator(&translator);

    QWidget widget;
    widget.setWindowTitle(QObject::tr("Hello World!"));
    widget.show();
    return app.exec();
}

SOURCES += \
    main.cpp

TRANSLATIONS += app_zh_CN.ts

The Gui interface is "Hello World!" also.. But in my file.qm is be translate to "你好!"(chinese)... where is the preblem ? who can help me..

like image 338
Mr.Tu Avatar asked Feb 10 '12 09:02

Mr.Tu


1 Answers

Your example works for me if I put the .qm file in the "correct" spot. (See below.) Make sure you are doing all the steps:

  1. Run lupdate to create the .ts file.
  2. Do your translation in Linguist and save the .ts file.
  3. Run lrelease to compile the .ts file to a .qm file.
  4. Copy the .qm file to the correct location.

My guess is that #4 is going bad. The documentation for QTranslator::load states:

If directory is not specified, the directory of the application's executable is used (i.e., as applicationDirPath()).

However, I had to put the .qm file in the folder above the executable to get it to work as is. Unless I'm misunderstanding the docs, this is a Qt bug, but one that is simple to workaround. If I explicitly gave the directory as app.applicationDirPath, it worked in the executable folder. You could also specify a separate directory. For example:

translator.load("app_zh_CN.qm"); works with:

[MyApp]
  app_zh_CN.qm
  [debug]
    MyApp.exe

translator.load("app_zh_CN.qm", app.applicationDirPath()); works with:

[MyApp]
  [debug]
    app_zh_CN.qm
    MyApp.exe
like image 170
Dave Mateer Avatar answered Oct 21 '22 16:10

Dave Mateer