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. QT_TR_NOOP does not get recognized from lupdate
Forum Updated to NodeBB v4.3 + New Features

QT_TR_NOOP does not get recognized from lupdate

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.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
    sandro4912
    wrote on last edited by sandro4912
    #1

    I have this code:

    #include <QApplication>
    #include <QMessageBox>
    #include <QTranslator>
    
    int main(int argc, char **argv)
    {
        QApplication app{argc, argv};
    
        QTranslator translator;
        translator.load("noop_de_DE");
        app.installTranslator(&translator);
    
        QVector<QString>texts = {
            QT_TRANSLATE_NOOP("main", "URL"),
            QT_TRANSLATE_NOOP("main", "Title"),
            QT_TRANSLATE_NOOP("main", "Publisher")
        };
    
        QVector<QString>texts2 = {
            QT_TR_NOOP("This is a very special string."),
            QT_TR_NOOP("And this is just as special")};
    
        QMessageBox::information( nullptr,
             qApp->translate("main",texts[2].toStdString().c_str()),
             qApp->translate(nullptr,texts2[1].toStdString().c_str()) );
    }
    

    If I run lupdate only the strings in QT_TRANSLATE_NOOP get added to the translation file. WHY QT_TR_NOOP is not recognized?

    It gives tr() cannot be called without context for the lines with QT_TR_NOOP

    Also it says in the help that qApp->translate() is obsolete bur does not mention whats the alternative in QT5.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      See https://doc.qt.io/qt-5/i18n-source-translation.html#using-qt-tr-noop-and-qt-translate-noop-in-c
      For QT_TR_NOOP you need a context (= class derived from QObject) where you define your strings to translate. So since you don't have a class here the correct way is to use QT_TRANSLATE_NOOP
      And why do you store your untranslated strings as QString at all? Simple char* would be enough and would save you the strange toStdString().c_str() stuff (which should be replaced to toUtf8().constData() if really needed...)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • S Offline
        S Offline
        sandro4912
        wrote on last edited by
        #3

        well i have it from an example in QT4 which looks like this:

        #include <QApplication>
        
        #include <QMessageBox>
        
        #include <QTranslator>
        
        int main( int argc, char **argv )
        {
          QApplication app( argc, argv );
          
          QTranslator translator;
          translator.load( "noop_sv_SE" );
          app.installTranslator( &translator );
          
          char *texts[] = { QT_TRANSLATE_NOOP("main","URL"),
                            QT_TRANSLATE_NOOP("main","Title"),
                            QT_TRANSLATE_NOOP("main","Publisher") };
                            
          char *texts2[] = { QT_TR_NOOP( "This is a very special string."),
                             QT_TR_NOOP( "And this is just as special.") };
          
          QMessageBox::information( 0, qApp->translate("main",texts[2]), qApp->translate(0,texts2[1]) );
        
          return 0;
        }
        

        I turn on c++17 compilation in the project. Than i get complains that in C++11 to convert from char* to string literals is not allowed. Also in this example code QT_TR_NOOP is used and seems to work. But its based on a old QT4 version.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Because your char strings must be const -> const char *texts[].
          QT_TR_NOOP can't work - see documentation.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          5

          • Login

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