Cannot load .qm translation file
-
Hello,
I'm new to QT so, if I do something wrong, please forgive me :p
I'm trying to do a small application to test the translation tools of QT.
I've generated the .qm file from the Linguist. I've put it in the resources. I can test that the file is there with the QFile::exists(":/portuguese.qm") which returns true- My main.cpp:
int main(int argc, char *argv[]) { QApplication a(argc, argv); QTranslator t; QStringList languages; languages << "English"<<"Portuguese"; QString lang = QInputDialog::getItem(NULL,"Select Language","Language", languages); if(!QFile::exists(":/portuguese.qm")) qWarning("failed-no file"); else qWarning("File Found"); if(lang == "Portuguese") { if ( t.load(":/portuguese.qm") ) { a.installTranslator(&t); qDebug() << "File loaded"; } else { qWarning() << "File not loaded"; } } if (lang != "English") { a.installTranslator(&t); } Widget w; w.show(); return a.exec(); }
- My translationfile.qrc
<RCC> <qresource prefix="/"> <file>portuguese.qm</file> </qresource> </RCC>
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ widget.cpp HEADERS += \ widget.h FORMS += \ widget.ui TRANSLATIONS += \ portuguese.ts # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target DISTFILES += \ portuguese.qm RESOURCES += \ translationfile.qrc
I've tried to load the .qm file in a lot of different ways, but non of them work. Any kind of help will be preciouses.
Best regards,
André Vaz -
You're using QTranslator::load() wrong.
-
I've saw in a quick YouTube tutorial, some guy just use it in that way. Any way, I've try to use the load method in a lot different ways and none of them work.
-
@andrezzvazz said in Cannot load .qm translation file:
Any way, I've try to use the load method in a lot different ways and none of them work.
Please show some code how you tried.
-
There you have it!
if(lang == "Portuguese") { qDebug() << t.load(":/portuguese.qm"); qDebug() << t.load(":/portuguese"); qDebug() << t.load("portuguese"); qDebug() << t.load(QLocale(), QLatin1String("portuguese"), QLatin1String(":/")); qDebug() << t.load(QLocale(), QLatin1String("portuguese.qm"), QLatin1String(":/")); }
Each one of these throw false...
-
They all can't work because you don't pass the correct directory (second parameter for the first three, forth parameter for the other two). prefix != directory!
-
@andrezzvazz you may want to check this answer in Stack Overflow.
Interesting is the part where you can set the language for every file into the resource file itself and then Qt load the proper file based on the locale in use.