Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Problem with the Hello tr() Example - translation does not show up.
Forum Updated to NodeBB v4.3 + New Features

[solved] Problem with the Hello tr() Example - translation does not show up.

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 9.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Trebor
    wrote on last edited by
    #1

    I have some trouble getting the "hello tr()" example to work, even though it should be pretty much forward.
    I followed every step from the documentation here: http://doc.qt.nokia.com/latest/linguist-hellotr.html

    My resulting "hellotr_la.ts" looks like this (yeah, I translated into German instead of Latin, but that should be no problem):
    @<?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE TS>
    <TS version="2.0" language="de_DE">
    <context>
    <name>QPushButton</name>
    <message>
    <location filename="main.cpp" line="63"/>
    <source>Hello world!</source>
    <translation>Hallo, Welt!</translation>
    </message>
    </context>
    </TS>@

    I created the "hellotr_la.qm" file by choosing File|Release from Qt Linguist's menu bar. Then I rebuilt the project, but the button text is still in English. I tried copying the file "hellotr_la.qm" to the output folder (is that necessary? It´s not mentioned on the documentation page), the button text is still in English.

    Since I´m pretty new to Qt (I´ve only been dabbling around with compiling and testing the examples for a couple of days) I´m sure I´m doing something wrong. But what?

    My OS is Windows Vista Home Edition and I´m using Qt Creator to build the examples.

    I didn´t make any changes to the example source itself.

    main.cpp:
    @#include <QApplication>
    #include <QPushButton>
    #include <QTranslator>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QTranslator translator;
    translator.load("hellotr_la");
    app.installTranslator(&translator);
    QPushButton hello(QPushButton::tr("Hello world!"));
    hello.resize(100, 30);
    hello.show();
    return app.exec();
    }@

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on last edited by
      #2

      Try
      @bool res = translator.load("hellotr_la");
      qDebug() << res;@

      Seems like you app can't find .qm file. You should keep it in folder with your app binary.

      --
      Vasiliy

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Trebor
        wrote on last edited by
        #3

        You should keep it in folder with your app binary.
        I already did that, but that had no effect (see OP).
        Anyways, I fixed the problem like this:

        @#include <QApplication>
        #include <QPushButton>
        #include <QTranslator>
        #include <QtDebug>
        #include <QDir>

        int main(int argc, char *argv[])
        {
        QApplication app(argc, argv);
        QTranslator translator;
        bool bFound = false;
        QDir dir(".");
        QString trPath;

        qDebug() << "Application path: " << dir.currentPath();
        qDebug() << "Binary path: " << QCoreApplication::applicationDirPath();
        trPath = QCoreApplication::applicationDirPath() + "/hellotr_la";
        bFound = translator.load(trPath);
        qDebug() << "Found hellotr_la: " << bFound;
        
        app.installTranslator(&translator);
        QPushButton hello(QPushButton::tr("Hello world!"));
        hello.resize(100, 30);
        hello.show();
        return app.exec&#40;&#41;;
        

        }@

        The debug output looks like this:
        @Application path: "C:/dev/QtSDK/Examples/4.7/linguist/hellotr-build-desktop-Qt_4_7_4_for_Desktop_-MinGW_4_4__Qt_SDK__Debug"
        Binary path: "C:/dev/QtSDK/Examples/4.7/linguist/hellotr-build-desktop-Qt_4_7_4_for_Desktop
        -_MinGW_4_4__Qt_SDK__Debug/debug"
        Found hellotr_la: true@

        So... when starting the program inside Qt Creator the current path is NOT set to the actual location of the binary. Which is kinda strange (maybe this only happens with Qt Creator Version 2.3.0 under Windows, I googled a bit and did not find tons of users complaining about that).

        And the example documentation fails to mention this behaviour. It also fails to mention that the file "hellotr_la.qm" needs to be copied to the location of the binary, but doing that is quite logical.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on last edited by
          #4

          You can ask creator to set the working directory in Projects->run settings.

          Depending on the working directory to pick up non-user files is in general not a good idea though.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Trebor
            wrote on last edited by
            #5

            You can ask creator to set the working directory in Projects->run settings.
            Yeah, I already found that out myself, but thanks for that hint. It might help other new users.

            Depending on the working directory to pick up non-user files is in general not a good idea though.
            I agree. But the application was not written by me, it comes with the SDK as a demo. In the ideal case an example program showcasing a specific feature (in this case the QTranslator) should work smoothly without forcing the user to make changes himself (in this case either to the source code or the Creator settings).

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Ade Malsasa Akbar
              wrote on last edited by
              #6

              [quote author="Vass" date="1316678123"]Seems like you app can't find .qm file. You should keep it in folder with your app binary.[/quote]

              Thank you for this clue, Mr Vass. I have same problem with TS but it works like charm for me. I moved the .qm file into the binary directory and the translation appears on the running app. Thank you.

              1 Reply Last reply
              0
              • H Offline
                H Offline
                houmingc
                wrote on last edited by
                #7

                i can run the example "hello world."
                but i cannot see the translation to another language.
                Please kindly explain where is the error make.

                ===Below is hellotr.pro=================
                SOURCES = main.cpp
                TRANSLATIONS = hellotr_la.ts
                target.path = /home/common/Qt5.3.1/Examples/Qt-5.3/linguist/hellotr
                INSTALLS += target sources
                QT += widgets

                ===Below is main.cpp==========================
                #include <QApplication>
                #include <QPushButton>

                #include <qdebug.h>
                #include <QTranslator>

                int main(int argc, char *argv[])

                {
                QApplication app(argc, argv);
                QTranslator translator;
                translator.load("hellotr_la");
                app.installTranslator(&translator);
                QPushButton hello(QPushButton::tr("Hello world!"));
                hello.resize(100, 30);
                hello.show();
                return app.exec();
                }

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  houmingc
                  wrote on last edited by
                  #8

                  i follow the link
                  http://qt.developpez.com/doc/4.7/linguist-hellotr/

                  did
                  lupdate -verbose hellotr.pro
                  linguist hellotr_la.ts
                  i check the xml translation type unfinished change to foreign language.

                  When i run the app the application is still Hello World

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    JvdGlind
                    wrote on last edited by
                    #9

                    [quote author="houmingc" date="1414045268"]
                    When i run the app the application is still Hello World[/quote]

                    Did you release the translation as .qm file?

                    Jeffrey VAN DE GLIND
                    Principle Consultant @ Nalys
                    www.nalys-group.com

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      houmingc
                      wrote on last edited by
                      #10

                      it solves thanks

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved