[SOLVED] translator does not install?
-
Hello,
I am having a very weird problem: I tried the arrowpad example of QT to test QtLinguist. It works fine. I wrote my own little example (even less code than arrowpard) and it didn't work. Here is my code:QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = testLinguist TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp TRANSLATIONS = testlinguist_en.ts \ testlinguist_de.ts \ testlinguist_fr.ts HEADERS += mainwindow.h RESOURCES += \ resources.qrc int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); QString locale = "fr" QTranslator translator; //bool success = translator.load(QString("testlinguist_") + locale); //did not work just placing the file in the same folder as pro file. bool success = translator.load(QString(":\\testlinguist_") + locale); a.installTranslator(&translator); return a.exec(); } MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QWidget* mainWidget = new QWidget(this); setCentralWidget(mainWidget); QPushButton* button = new QPushButton(tr("&Press")); QGridLayout *mainLayout = new QGridLayout(mainWidget); mainLayout->addWidget(button, 0, 1); mainWidget->setLayout(mainLayout); }
at first the success variable in main.cpp was false (I fixed that by putting the qm file in the resources). Now it loads the qm file (success is true), but there still are no translations made. I did everything exactly like in the arrowpad example.
Does anybody have any idea what could be going wrong here?
Thanks for your help! -
Hi,
On a side note, please use forward slashes in path, that will make your code cleaner and less error prone.
Do you only have one text to translate ?
-
I just did some deeper debugging. It seems like the translation file is loaded, but when executing the function tr() this line returns false
!self->d_func()->translators.isEmpty()
line 1967 in file qcoreapplication.cpp, function
QString QCoreApplication::translate(const char *context, const char *sourceText, const char *disambiguation, Encoding encoding, int n)
does someone know what I might be doing wrong?
I opened the .ts file the lupdate testLinguist.pro produces, I inserted the translations, clicked on the mark as finished icon (the checksign for this text turned yellow), and I ran lrelease testLinguist.pro to get the .qm files (which are loaded successfully according to the return variable success). I just don't know what is going wrong -
So showing it before loading the translator ?
One thing you can to is implement dynamic translation especially since you are providing several translations.