Results 1 to 5 of 5

Thread: how to use tr() for QMessageBox?

  1. #1
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default how to use tr() for QMessageBox?

    I'm displaying a simple Yes/No QMessageBox. This works fine in English. However, I want to internationalize this for French, so that it displays Oui/Non instead. Is there an easy way to change the text in the buttons for Yes/No?
    Software Engineer



  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to use tr() for QMessageBox?

    There's no easy way, but a standard one: http://doc.trolltech.com/4.3/i18n.ht...e-translations

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to use tr() for QMessageBox?

    Hi,

    Qt Code:
    1. QMessageBox messageBox(tr("Info"),
    2. tr("My message translated"),
    3. QMessageBox::Question,
    4. QMessageBox::Yes | QMessageBox::Default,
    5. QMessageBox::Cancel | QMessageBox::Escape, //This makes "Esc" key to Cancel the Dialog
    6. QMessageBox::NoButton,
    7. this);
    8. //Set the translation text of the buttons. Here you need to have a QTranslator installed that will translate the "Yes" and "No" texts:
    9. messageBox.setButtonText(QMessageBox::Yes,tr("Yes"));
    10. messageBox.setButtonText(QMessageBox::Cancel,tr("Cancel"));
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  4. The following user says thank you to ^NyAw^ for this useful post:

    gfunk (20th November 2007)

  5. #4
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: how to use tr() for QMessageBox?

    What version of Qt are you using?

    This should work for 4.2.2 (it does for me):

    Qt Code:
    1. QT_TRANSLATE_NOOP("QDialogButtonBox", "&Yes");
    2. QT_TRANSLATE_NOOP("QDialogButtonBox", "&No");
    To copy to clipboard, switch view to plain text mode 

    Check QDialogButtonBox.cpp for other strings to translate

  6. The following user says thank you to Byngl for this useful post:

    gfunk (20th November 2007)

  7. #5
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to use tr() for QMessageBox?

    You should load a Qt library translator. There are some translators in %QTDIR%\translations which translate every user visible text in Qt library. French one is incomplete, but you may modify it in linguist.

    Qt Code:
    1. QTranslator *qtTranslator = new QTranslator;
    2. QTranslator *appTranslator = new QTranslator;
    3. qtTranslator->load(
    4. QString("qt_%1").arg(lang),
    5. QLibraryInfo::location(QLibraryInfo::TranslationsPath)
    6. );
    7. appTranslator->load(...); // Load your translator
    8.  
    9. installTranslator(appTranslator);
    10. installTranslator(qtTranslator);
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.