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]QNetworkAccessManager, QNetworkReply errorString() translation
QtWS25 Last Chance

[SOLVED]QNetworkAccessManager, QNetworkReply errorString() translation

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 9.2k Views
  • 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.
  • S Offline
    S Offline
    sidewinder
    wrote on last edited by
    #1

    My first post on this board so... Hello everyone :)

    I can't get -QNetworkAccessManager- QNetworkReply to return translated error strings. Code below shows an example of my problem:

    @#include <QtCore/QCoreApplication>
    #include <QNetworkReply>
    #include <QNetworkAccessManager>
    #include <QTcpSocket>
    #include <QDebug>
    #include <QTranslator>
    #include <QLibraryInfo>

    class Test : public QObject
    {
    Q_OBJECT
    public:
    Test(QObject *parent = 0): manager_(new QNetworkAccessManager()),socket_(new QTcpSocket()){}
    ~Test(){delete this->manager_;delete this->socket_;}
    void startConnection()
    {
    this->reply_ = this->manager_->get(QNetworkRequest(QUrl("http:///127.0.0.1/error.html")));
    this->connect(reply_,SIGNAL(finished()),SLOT(process()));
    this->socket_->connectToHost("127.0.0.1",1);
    if (!this->socket_->waitForConnected(3000)){
    qDebug() << this->socket_->errorString();
    }
    }

    signals:
    void finished();

    private:
    QNetworkAccessManager* manager_;
    QNetworkReply* reply_;
    QTcpSocket* socket_;

    private slots:
    void process()
    {
    qDebug() << reply_->errorString();
    this->reply_->deleteLater();
    emit finished();
    }
    };

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    QTranslator qtTranslator;
    qtTranslator.load("qt_pl",QLibraryInfo::location(QLibraryInfo::TranslationsPath));
    a.installTranslator(&qtTranslator);

    Test test;
    test.startConnection();
    test.connect(&test,SIGNAL(finished()),&a,SLOT(quit()));
    
    return a.exec(&#41;;
    

    }
    #include "moc_main.cpp"@

    I tried loading German,Spanish and Italian translations as well. errorString from QTcpSocket is always translated to appropriate language but errorString from -QNetworkAccessManager- QNetworkReply is always in English. Am I doing something wrong or that's a missing translation bug?

    I'm using Qt 4.7.2 on windows 7.

    In order to compile code above you have to run moc manually (moc main.cpp > moc_main.cpp) as I put everything in one file.

    Edit: Messed up QNetworkAccessManager with QNetworkReply. Fixed.

    "Never memorize what you can look up in books."
    Albert Einstein

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

      your description is not correct, QNetworkAccessManager has no method errorString(), so it can't be translated :-)

      I suppose you mean QNetworkReply::errorString().

      If you look at the code of QNetworkReply, you see that the string is set from somewhere else :-(.
      If you scan the code, it seems the errors are translated there...

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sidewinder
        wrote on last edited by
        #3

        Right, I messed up classes names. Already fixed that. Thanks.

        I'd searched for translations in ts files and I found some error strings translated but in QAbstractSocket (which is direct parent of QTcpSocket). I wasn't able to find any relevant translations neither in QNetworkReply nor in QIODevice...

        "Never memorize what you can look up in books."
        Albert Einstein

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          It is not translated via QIODevice, it uses QCoreApplication::translate

          Its in the classes, not directly in QNetworkReply. QNetworkReply has a method setError(...) which is called by QNetwork... (forgotten the exact class, read the classes QNetworkAccessManager and used classes).

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dangelog
            wrote on last edited by
            #5

            Those strings are set by Qt and are localizable. All you need to do is to set up your translator to load Qt translations as well (and eventually provide those translations for your language :-)). See what's inside $QTDIR/translations.

            Software Engineer
            KDAB (UK) Ltd., a KDAB Group company

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sidewinder
              wrote on last edited by
              #6

              Lines 50 and 51 are loading Qt translations. I'd be happy to translate missing error strings but I can't find them. Loading qt_pl.ts into Linguist shows only a few untranslated entries and none of them is connected with network context. Moreover it seems that QNetworkReply::errorString() returns english error string no matter which translation I try (wrote about that in first post). I'm sure Qt translations are loaded correctly because QTcpSocket error strings are translated.

              "Never memorize what you can look up in books."
              Albert Einstein

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                Hi Pepe,
                that was exactly what he did. Look at line 5:

                @
                int main(int argc, char *argv[])
                {
                QCoreApplication a(argc, argv);
                QTranslator qtTranslator;
                qtTranslator.load("qt_pl",QLibraryInfo::location(QLibraryInfo::TranslationsPath));
                a.installTranslator(&qtTranslator);

                Test test;
                test.startConnection();
                test.connect(&test,SIGNAL(finished()),&a,SLOT(quit()));
                
                return a.exec(&#41;;
                

                }
                @

                And he said, for other classes he gets correct translated strings.
                It should work if Qt is build and installed correctly.

                @ sidewinder: did you check whether qtTranslator.load works fine?

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sidewinder
                  wrote on last edited by
                  #8

                  Yep. qtTranslator.load(...) returns true.
                  All other translations like context menus, other classes error strings, widgets default tooltips are loaded correctly.
                  To be 101% sure I've just added new line to main function:
                  @qDebug() << qtTranslator.translate("QHttp","Unknown error");@
                  Which prints another line in console with correct translation.

                  "Never memorize what you can look up in books."
                  Albert Einstein

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dangelog
                    wrote on last edited by
                    #9

                    You're absolutely right, my fault. I'm investigating the problem.

                    Software Engineer
                    KDAB (UK) Ltd., a KDAB Group company

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dangelog
                      wrote on last edited by
                      #10

                      Qt bug. The strings in here

                      http://qt.gitorious.org/qt/qt/blobs/master/src/network/access/qhttpnetworkconnection.cpp#line637

                      are not translated (since QT_TRANSLATE_NOOP marks them for translation, but doesn't actually translate them).

                      Software Engineer
                      KDAB (UK) Ltd., a KDAB Group company

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        dangelog
                        wrote on last edited by
                        #11

                        I've filed a bug and submitted a patch here. http://bugreports.qt.nokia.com/browse/QTBUG-18382

                        Software Engineer
                        KDAB (UK) Ltd., a KDAB Group company

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          sidewinder
                          wrote on last edited by
                          #12

                          Great! Thanks for help peppe and Gerolf. I'm going to recompile Qt right away. :)

                          "Never memorize what you can look up in books."
                          Albert Einstein

                          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