[Interest] Embedding language translation as resource

Robert Hairgrove evorgriahr at hispeed.ch
Tue Apr 27 07:31:15 CEST 2021


You need to load the ".qm" file which is the binary generated by running 
lrelease, not the ".ts" file.

In addition to your "hello_la.qm" file, you might want to load Qt's 
"qtbase_la.qm" file (if there is one) which contains translations for 
most of the built-in GUI elements.

HTH, Bob

--

On 27.04.21 07:06, Nicholas Yue wrote:
> Hi,
>
>   I am trying to embed translation file as a resource but when I run 
> the compiled program, it fails to load the translation
>
> ERROR: Translation failure to load
>
> main.cpp
> ***********
> #include <QApplication>
> #include <QPushButton>
> #include <QTranslator>
> #include <iostream>
>
> int main(int argc, char *argv[])
> {
>     QApplication app(argc, argv);
>     QString appDir = QCoreApplication::applicationDirPath();
>     QTranslator translator;
>     QString translationDir = appDir + "/translation";
>     // if (translator.load("hellotr_la",translationDir)) {
> if (translator.load(":/language/hellotr_la.ts")) {
>     app.installTranslator(&translator);
>     } else {
>     std::cerr << "ERROR: Translation failure to load" << std::endl;
>     }
>
>     QPushButton hello(QPushButton::tr("Hello world!"));
>     hello.resize(100, 30);
>
>     hello.show();
>     return app.exec();
> }
>
> hellotr_lang.qrc
> ******************
> <!DOCTYPE RCC><RCC version="1.0">
> <qresource prefix="/language">
>     <file>hellotr_la.ts</file>
> </qresource>
> </RCC>
>
> hellotr_la.ts
> *************
> <?xml version="1.0" encoding="utf-8"?>
> <!DOCTYPE TS>
> <TS version="2.1" language="la_VA">
> <context>
>     <name>QPushButton</name>
>     <message>
>         <location filename="main.cpp" line="19"/>
>         <source>Hello world!</source>
>         <translation type="unfinished"></translation>
>     </message>
> </context>
> </TS>
>
> hellotr,pro
> ************
> SOURCES      = main.cpp
> TRANSLATIONS = hellotr_la.ts
>
> target.path = $$[QT_INSTALL_EXAMPLES]/linguist/hellotr
> INSTALLS += target
>
> QT += widgets
>
> RESOURCES += hellotr_lang.qrc
>
>
> Cheers
> -- 
> Nicholas Yue
> Graphics - Arnold, Alembic, RenderMan, OpenGL, HDF5
> Custom Dev - C++ porting, OSX, Linux, Windows
> http://au.linkedin.com/in/nicholasyue
> https://vimeo.com/channels/naiadtools
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210427/00d01f88/attachment.html>


More information about the Interest mailing list