Embed qt translation into executable in static app
-
Hello!
I want to embed the
Qt
translation files like:qt_en.qm
,qt_uk.qm
into my executable using resource. I have added it to the resource directory named Localization.bool isLoaded = qtTranslator.load(QString(":Localization/qt_uk.qm")); qDebug() << "isLoaded: " << isLoaded;
It returns
false
. When I add my translation file, for example:test_uk.qm
then it loads successfully. So, how to embed theQt
translations files into the executable (static program)?I think, I have found the issue,
qt_uk.qm
file size is only1 KB
, but when I check out for the same file for other program (shared), the file size is155 KB
. So, it means thatQt
static also embeds the Qt localization files automatically? Then how to load them usingQTranslator
?For example, I translated my program to ukrainian language but some widgets like
QMessageBox
buttons andQTreeView
header items are still not translated:This translation comes from Qt files not mine. I can call
setButtonText
to translate the QMessageBox buttons, but how to translate theQTreeView
header items like (Name, Size, Type, Date modified) which added automatically byQFileSystemModel
? There should be some way to call the default Qt translation for this?
Thanks. -
Hello!
This only works when compiling the program with
Qt
shared libs. Using static build I do not have any default Qt translation (qt_*
) files. The Qt installation dir contains the translations files but all of them are1 KB
.Ok. I have fixed the issue by inheriting the
QFileSystemModel
and overriding theheaderData
method. Now, it works as expected:The issue is resolved. Thank you.
-
Hello!
I want to embed the
Qt
translation files like:qt_en.qm
,qt_uk.qm
into my executable using resource. I have added it to the resource directory named Localization.bool isLoaded = qtTranslator.load(QString(":Localization/qt_uk.qm")); qDebug() << "isLoaded: " << isLoaded;
It returns
false
. When I add my translation file, for example:test_uk.qm
then it loads successfully. So, how to embed theQt
translations files into the executable (static program)?I think, I have found the issue,
qt_uk.qm
file size is only1 KB
, but when I check out for the same file for other program (shared), the file size is155 KB
. So, it means thatQt
static also embeds the Qt localization files automatically? Then how to load them usingQTranslator
?For example, I translated my program to ukrainian language but some widgets like
QMessageBox
buttons andQTreeView
header items are still not translated:This translation comes from Qt files not mine. I can call
setButtonText
to translate the QMessageBox buttons, but how to translate theQTreeView
header items like (Name, Size, Type, Date modified) which added automatically byQFileSystemModel
? There should be some way to call the default Qt translation for this?
Thanks.I think, the only way is to subclass
QFileSystemModel
and override theheaderData
method.QVariant QFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const { switch (role) { case Qt::DecorationRole: if (section == 0) { // ### TODO oh man this is ugly and doesn't even work all the way! // it is still 2 pixels off QImage pixmap(16, 1, QImage::Format_Mono); pixmap.fill(0); pixmap.setAlphaChannel(pixmap.createAlphaMask()); return pixmap; } break; case Qt::TextAlignmentRole: return Qt::AlignLeft; } if (orientation != Qt::Horizontal || role != Qt::DisplayRole) return QAbstractItemModel::headerData(section, orientation, role); QString returnValue; switch (section) { case 0: returnValue = tr("Name"); break; case 1: returnValue = tr("Size"); break; case 2: returnValue = #ifdef Q_OS_MAC tr("Kind", "Match OS X Finder"); #else tr("Type", "All other platforms"); #endif break; // Windows - Type // OS X - Kind // Konqueror - File Type // Nautilus - Type case 3: returnValue = tr("Date Modified"); break; default: return QVariant(); } return returnValue; }
But it has
tr()
method on these items, so it should be already translated.
When program compiled usingQt
shared libs, I can call this:QString appPath = QApplication::applicationDirPath(); qtTranslator->load(QString("%1/translations/qt_uk.qm").arg(appPath));
to load the default Qt translations and it works well. The question is how to call/trigger it from the app which compiled statically?
-
Hi, I think the problem is only to use the right path in the .pro and Resources.qrc file. Please have a look:
.pro file
RESOURCES += \ Resources.qrc
main.cpp
QTranslator translator, translator2; if (translator.load("QtComPort_"+ QLocale::system().name(), ":/Translations")) a.installTranslator(&translator); if (translator2.load("qt_"+ QLocale::system().name(), ":/Translations")) a.installTranslator(&translator2);
Resource.qrc
<RCC> <qresource prefix="/Translations"> <file>QtComPort_it.qm</file> <file>qt_it.qm</file> </qresource> </RCC>
-
Hi, I think the problem is only to use the right path in the .pro and Resources.qrc file. Please have a look:
.pro file
RESOURCES += \ Resources.qrc
main.cpp
QTranslator translator, translator2; if (translator.load("QtComPort_"+ QLocale::system().name(), ":/Translations")) a.installTranslator(&translator); if (translator2.load("qt_"+ QLocale::system().name(), ":/Translations")) a.installTranslator(&translator2);
Resource.qrc
<RCC> <qresource prefix="/Translations"> <file>QtComPort_it.qm</file> <file>qt_it.qm</file> </qresource> </RCC>
Hello!
This only works when compiling the program with
Qt
shared libs. Using static build I do not have any default Qt translation (qt_*
) files. The Qt installation dir contains the translations files but all of them are1 KB
. -
Hello!
This only works when compiling the program with
Qt
shared libs. Using static build I do not have any default Qt translation (qt_*
) files. The Qt installation dir contains the translations files but all of them are1 KB
.Ok. I have fixed the issue by inheriting the
QFileSystemModel
and overriding theheaderData
method. Now, it works as expected:The issue is resolved. Thank you.
-
C Cobra91151 has marked this topic as solved on
-
Ok. I have fixed the issue by inheriting the
QFileSystemModel
and overriding theheaderData
method. Now, it works as expected:The issue is resolved. Thank you.
Hello!
I have found out that even when I inherit
QFileSystemModel
and override theheaderData
method, some of the data are still not translated. So, my new solution is the following.- Compile the program as shared build.
- Get the required default Qt translation (
qt_*
) files. - Store them to the resource file
- Load them together with my translations.
Now, it translates all data. This issue is solved.
-
Hello!
I have found out that even when I inherit
QFileSystemModel
and override theheaderData
method, some of the data are still not translated. So, my new solution is the following.- Compile the program as shared build.
- Get the required default Qt translation (
qt_*
) files. - Store them to the resource file
- Load them together with my translations.
Now, it translates all data. This issue is solved.
@Cobra91151 said in Embed qt translation into executable in static app:
Get the required default Qt translation (qt_*) files.
This seems like the proper solution.