Load .qm file from resource
-
Hi,
i try to load a .qm file from my resource:int main(int argc, char *argv[]) { Q_INIT_RESOURCE(resource); QApplication app(argc, argv); QTranslator translator; if (translator.load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLatin1String(":/translations"))) app.installTranslator(&translator); MainWindow w; w.showMaximized(); return app.exec(); }
The resource.qrc
<RCC> <qresource prefix="/translations"> <file>resources/qt_de.qm</file> </qresource> </RCC>
But the load function always returns false.
Where is my mistake?
Thanks -
I would check what QLocale() returns
If you look here
http://wiki.qt.io/How_to_create_a_multi_language_applicationThey seem to use
QString defaultLocale = QLocale::system().name(); // e.g. "de_DE"
defaultLocale.truncate(defaultLocale.lastIndexOf('_')); // e.g. "de"So maybe its just a syntax mismatch as QLocale() might return as "de_DE"
or something else. -
So i tried it all, but the QTranslator won't load the file.
It's strange beacuase i can load and open the file with QFile.
QFile file; file.setFileName(":/translations/resources/qt_de.qm"); qDebug() << file.open(QIODevice::ReadOnly); // true qDebug() << translator.load(":/translations/resources/qt_de.qm"); // false
-
@beecksche said in Load .qm file from resource:
qDebug() << translator.load("qt_de", ":/translations/resources");
?
-
My test results
QFile file; file.setFileName(":/translations/resources/qt_de.qm"); qDebug() << file.open(QIODevice::ReadOnly); qDebug() << translator.load(QLatin1String(":/translations/resources/qt_de")); qDebug() << translator.load(QLatin1String(":/translations/resources/qt_de.qm")); qDebug() << translator.load(QLatin1String("qt_de"), QLatin1String(":/translations")); qDebug() << translator.load(QLatin1String("qt_de"), QLatin1String(":/translations/resources")); qDebug() << translator.load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLatin1String(":/translations")); qDebug() << translator.load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLatin1String(":/translations/resources"));
true false false false false false false
-
QFile file; file.setFileName("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations/qt_de.qm"); qDebug() << file.open(QIODevice::ReadOnly); qDebug() << translator.load(QLatin1String("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations/qt_de")); qDebug() << translator.load(QLatin1String("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations/qt_de.qm")); qDebug() << translator.load(QLatin1String("qt_de"), QLatin1String("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations")); qDebug() << translator.load(QLocale(), QLatin1String("qt"), QLatin1String("_"), QLatin1String("C:/Qt/Qt5.7.0_MSVC2013/5.7/msvc2013/translations"));
true false false true true
If i use the file outside the resource i can load it!
-
@beecksche
the problem is the default compression of qrc.
QTranslator checks for qrc files and only loads them when they are uncompressed (don't ask me whats the reason for that is).Try to add the following to your .pro file:
QMAKE_RESOURCE_FLAGS += -no-compress
-
@raven-worx
Thanks for the information. I addedQMAKE_RESOURCE_FLAGS += -no-compress
Run qmake and build it again, but still not able to load it from resource.
-
As a referense: nice article by @ekkescorner : https://appbus.wordpress.com/2016/04/28/howto-translations-i18n/
Try moving the .qm files in the main directory rather than the resources one and see if that works
-
@beecksche
can you try this workaround:- create a .rcc file with
rcc -no-compress mytranslations.qrc -o mytranslations.rcc
- add this .rcc file to your qrc file
- load the .rcc file with
QFile translations(":/mytranslations.rcc"); translations.open(QIODevice::ReadOnly); QResource::registerResource( (const uchar *)translations.readAll().constData() ); translator.load( ... );
- create a .rcc file with
-
I am having the same problem. I found the following page
http://doc.qt.io/qt-5/linguist-programmers.html which describes using lconvert to create a qt_de.qm file with only the required modules, e.g.:lconvert -o installation_folder/qt_de.qm qtbase_de.qm qtdeclarative_de.qm
I found that if I created the .qm file in my resource file this way, I could then load the qt translation from my resource file.
It is interesting that the size of qt_de.qm in C:/Qt/Qt5.7.0/5.7/mingw53_32/translations is 1 KB. The qt_de.qm that is generated by lconvert is 239 KB.
-
Probably the problem in prefix. Here are my files from working project
resources.qrc
<RCC> <qresource prefix="/"> <file>assets/translations/language_ru.qm</file> <file>assets/translations/language_en.qm</file> </qresource> </RCC>
Folders structure:
ProjectRoot/project.pro
ProjectRoot/resources.qrc
ProjectRoot/assets/translations/language_ru.qm
ProjectRoot/assets/translations/language_ru.qmCode that works ok
QTranslator tr; tr.load(":/assets/translations/language_ru.qm")
-
I would like to clarify something. I do not think that the problem the original poster has is in the prefix. As I said, I believe I was having the same problem. My application's translation file, dwb_lang_de.qm, can be loaded by QTranslator without any problem. My problem came about when I tried to load Qt's translation file, qt_de.qm. If you look at the Qt translation files, (I'm running on windows so they are in C:\Qt\Qt5.7.0\5.7\mingw53_32\translations), you will see that qt_de.qm is suspiciously small. In fact, the Qt Linguist Manual (URL is above) states:
In Qt 5, the .qm files were split up by module and there is a so-called meta catalog file which includes the .qm files of all modules. The name of the meta catalog file is identical to the name of Qt 4's monolithic .qm file so that existing loader code works as before provided all included .qm files are found.A solution is to use lconvert, as I suggested above, to build your own qt_de.qm that contains the strings for the modules that you need. This can then be added to your resource file and QTranslator can load it without any problem.
Since posting that, I have found that an alternate solution is to put all of the .qm files for Qt (assistant_cs.qm, assistant_da.qm, assistant_de.qm, ...) into your resource file. Then when you load the meta-file, qt_de.qm, it will load fine because it will find all the files it references.
Either method works fine.