#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..
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:
lupdate
to create the .ts file.lrelease
to compile the .ts file to a .qm file.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With