[solved] Problem with the Hello tr() Example - translation does not show up.
-
I have some trouble getting the "hello tr()" example to work, even though it should be pretty much forward.
I followed every step from the documentation here: http://doc.qt.nokia.com/latest/linguist-hellotr.htmlMy resulting "hellotr_la.ts" looks like this (yeah, I translated into German instead of Latin, but that should be no problem):
@<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="de_DE">
<context>
<name>QPushButton</name>
<message>
<location filename="main.cpp" line="63"/>
<source>Hello world!</source>
<translation>Hallo, Welt!</translation>
</message>
</context>
</TS>@I created the "hellotr_la.qm" file by choosing File|Release from Qt Linguist's menu bar. Then I rebuilt the project, but the button text is still in English. I tried copying the file "hellotr_la.qm" to the output folder (is that necessary? It´s not mentioned on the documentation page), the button text is still in English.
Since I´m pretty new to Qt (I´ve only been dabbling around with compiling and testing the examples for a couple of days) I´m sure I´m doing something wrong. But what?
My OS is Windows Vista Home Edition and I´m using Qt Creator to build the examples.
I didn´t make any changes to the example source itself.
main.cpp:
@#include <QApplication>
#include <QPushButton>
#include <QTranslator>int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTranslator translator;
translator.load("hellotr_la");
app.installTranslator(&translator);
QPushButton hello(QPushButton::tr("Hello world!"));
hello.resize(100, 30);
hello.show();
return app.exec();
}@ -
Try
@bool res = translator.load("hellotr_la");
qDebug() << res;@Seems like you app can't find .qm file. You should keep it in folder with your app binary.
-
You should keep it in folder with your app binary.
I already did that, but that had no effect (see OP).
Anyways, I fixed the problem like this:@#include <QApplication>
#include <QPushButton>
#include <QTranslator>
#include <QtDebug>
#include <QDir>int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTranslator translator;
bool bFound = false;
QDir dir(".");
QString trPath;qDebug() << "Application path: " << dir.currentPath(); qDebug() << "Binary path: " << QCoreApplication::applicationDirPath(); trPath = QCoreApplication::applicationDirPath() + "/hellotr_la"; bFound = translator.load(trPath); qDebug() << "Found hellotr_la: " << bFound; app.installTranslator(&translator); QPushButton hello(QPushButton::tr("Hello world!")); hello.resize(100, 30); hello.show(); return app.exec();
}@
The debug output looks like this:
@Application path: "C:/dev/QtSDK/Examples/4.7/linguist/hellotr-build-desktop-Qt_4_7_4_for_Desktop_-MinGW_4_4__Qt_SDK__Debug"
Binary path: "C:/dev/QtSDK/Examples/4.7/linguist/hellotr-build-desktop-Qt_4_7_4_for_Desktop-_MinGW_4_4__Qt_SDK__Debug/debug"
Found hellotr_la: true@So... when starting the program inside Qt Creator the current path is NOT set to the actual location of the binary. Which is kinda strange (maybe this only happens with Qt Creator Version 2.3.0 under Windows, I googled a bit and did not find tons of users complaining about that).
And the example documentation fails to mention this behaviour. It also fails to mention that the file "hellotr_la.qm" needs to be copied to the location of the binary, but doing that is quite logical.
-
You can ask creator to set the working directory in Projects->run settings.
Depending on the working directory to pick up non-user files is in general not a good idea though.
-
You can ask creator to set the working directory in Projects->run settings.
Yeah, I already found that out myself, but thanks for that hint. It might help other new users.Depending on the working directory to pick up non-user files is in general not a good idea though.
I agree. But the application was not written by me, it comes with the SDK as a demo. In the ideal case an example program showcasing a specific feature (in this case the QTranslator) should work smoothly without forcing the user to make changes himself (in this case either to the source code or the Creator settings). -
[quote author="Vass" date="1316678123"]Seems like you app can't find .qm file. You should keep it in folder with your app binary.[/quote]
Thank you for this clue, Mr Vass. I have same problem with TS but it works like charm for me. I moved the .qm file into the binary directory and the translation appears on the running app. Thank you.
-
i can run the example "hello world."
but i cannot see the translation to another language.
Please kindly explain where is the error make.===Below is hellotr.pro=================
SOURCES = main.cpp
TRANSLATIONS = hellotr_la.ts
target.path = /home/common/Qt5.3.1/Examples/Qt-5.3/linguist/hellotr
INSTALLS += target sources
QT += widgets===Below is main.cpp==========================
#include <QApplication>
#include <QPushButton>#include <qdebug.h>
#include <QTranslator>int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTranslator translator;
translator.load("hellotr_la");
app.installTranslator(&translator);
QPushButton hello(QPushButton::tr("Hello world!"));
hello.resize(100, 30);
hello.show();
return app.exec();
} -
i follow the link
http://qt.developpez.com/doc/4.7/linguist-hellotr/did
lupdate -verbose hellotr.pro
linguist hellotr_la.ts
i check the xml translation type unfinished change to foreign language.When i run the app the application is still Hello World
-
[quote author="houmingc" date="1414045268"]
When i run the app the application is still Hello World[/quote]Did you release the translation as .qm file?
-
it solves thanks