Unable to translate QAction text (empty string shown)



  • 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


  • Moderators

    there are many possible mistakes.

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



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


Log in to reply
 

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