[Interest] Embedding language translation as resource

Nicholas Yue yue.nicholas at gmail.com
Tue Apr 27 07:06:21 CEST 2021


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210426/858f02ea/attachment.html>


More information about the Interest mailing list