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. Unable to translate QAction text (empty string shown)
QtWS25 Last Chance

Unable to translate QAction text (empty string shown)

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.5k 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.
  • W Offline
    W Offline
    weirdal
    wrote on last edited by
    #1

    Hi,

    I have got a QtDesigner built application where I add a language menu at runtime (pretty much like in the examples).

    The menu should show the names of the languages in their native language (eg. English = "English", German = "Deutsch", Spanish = "Espanol", etc).

    The QActions are added to a QActionGroup at runtime in a createLanguageMenu() method. But action->setText() shows only empty strings...
    It works when I set the display text to the locale based (english) name of the respective language.

    How can I get those entries to show the correct display text (native name)?
    And is there anything special needed to display "exotic" language names as Chinese or Hebrew in their native writing?

    If you need more information, I'll post as required...

    Thank you,
    weirdal

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      there are many possible mistakes.

      Please post some code (especially where you load the translator and retranslate the gui on a translator change).

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • W Offline
        W Offline
        weirdal
        wrote on last edited by
        #3

        Thank you for responding.

        Qt version is 4.7.0.
        I have used the following article as a guideline in addition to the samples and tutorials from this site:
        "Dynamic Language Switching":http://www.informit.com/articles/article.aspx?p=1405555&seqNum=3

        I create the language menu in my MainWindow class (derived from QMainWindow):
        @void MainWindow::createLanguageMenu()
        {
        QAction *action;
        QString locale;

        // get reference to languages menu entry
        QMenu *menuLang = menuBar()->findChild<QMenu *>("menuLanguages");
        
        // create new action group
        QActionGroup *langGroup = new QActionGroup(menuLang);
        langGroup->setExclusive(true);
        
        // attach event handler
        connect(langGroup, SIGNAL(triggered(QAction *)), this, SLOT(slotLanguageChanged(QAction *)));
        
        // read files from directory
        QDir dir(qApp->applicationDirPath().append("/languages"));
        QStringList fileList = dir.entryList(QStringList("*.qm"));
        
        // iterate through list and create an entry for every language file found
        QTranslator translator;
        for (int i = 0; i < fileList.size(); i++)
        {
            translator.load(fileList[i]);
            QString displayName = translator.translate("MainWindow", "languageDisplayName");
        
            // extract locale from file name
            locale = fileList[i];
            locale.truncate(locale.lastIndexOf('.'));
            locale.remove(0, locale.lastIndexOf('_') + 1);
        
            action = createLocaleAction(locale, i + 1, displayName);
            menuLang->addAction(action);
            langGroup->addAction(action);
        
            if (m_currLang == locale)
                action->setChecked(true);
        }
        

        }@

        The actions are created in a seperate function:
        @QAction* MainWindow::createLocaleAction(const QString &locale, int pos, const QString &displayName)
        {
        QString lang = QLocale::languageToString(QLocale(locale).language());
        //QAction *action = new QAction(displayName, this);
        QAction *action = new QAction(lang, this);
        action->setCheckable(true);
        action->setData(locale);
        action->setStatusTip(QString(tr("Switch to ")).append(lang));
        action->setToolTip(QString(tr("Switch to ")).append(lang));

        return action;
        

        }@

        However, when I use the "displayName" parameter (which should represent the native language name) to set the action text, nothing is shown.
        !http://img14.imageshack.us/img14/1051/okng.png(menu_empty)!

        When I use the "lang" String extracted from a QLocale object, texts are displayed correctly:
        !http://img543.imageshack.us/img543/613/hmuz.png(menu_ok)!

        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