[SOLVED]QNetworkAccessManager, QNetworkReply errorString() translation



  • 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.



  • 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...



  • 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...



  • 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).



  • 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.



  • 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.



  • 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?



  • 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.



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



  • 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).



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.