2

QFileDialog is used in my code like following:

QFileDialog fileDlg;
fileDlg.setFileMode(QFileDialog::AnyFile);
fileDlg.setViewMode(QFileDialog::List);
fileDlg.setNameFilter("Excel Files(*.csv)");
fileDlg.setDefaultSuffix("csv");
fileDlg.setAcceptMode(QFileDialog::AcceptSave);
fileDlg.exec();

Unfortunately, this does not use text from the user's current locale. I would expect the save button to be "保存". Further, when I click on a dialog, the button's text is set to "Open", while it should be "打开" in my locale.

How can I provide localized strings to QFileDialog?

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
mac.ma
  • 703
  • 2
  • 8
  • 22
  • What would you expect it to be when selecting a file for saving and a directory is selected? You cannot overwrite a directory with a file. – laalto Jan 18 '12 at 09:04
  • yes,but when I clicked a Dir ,the text of Saving button will be changed to system language like "Open" .In my project,language must be uniform.for example: the language in my project is chinese,the text of saving button is "保存".At the same time,when clicked a Dir ,it should be "打开" not the "Open". – mac.ma Jan 18 '12 at 09:37
  • never use a QObject (and its subclasses) on the stack or disaster will happen. Always use `new`, ie `QFileDialog* fileDlg = new QFileDialog()`. – UmNyobe Jan 18 '12 at 17:27
  • 4
    UmNyobe: This isn't true. It's a modal dialog and will have closed before it goes out of scope. There's no problem with it. – Dan Milburn Jan 18 '12 at 17:33

3 Answers3

5

I know this has been answered and accepted but the correct way is not to manually translate what Qt already provides but rather to load the translations included in Qt like this:

 /* load the system translations provided by Qt */
 QTranslator qtTranslator;
 qtTranslator.load("qt_" + QLocale::system().name(),
         QLibraryInfo::location(QLibraryInfo::TranslationsPath));
 app.installTranslator(&qtTranslator);

 /* load our custom translations for this application */
 QTranslator myappTranslator;
 myappTranslator.load("myapp_" + QLocale::system().name());
 app.installTranslator(&myappTranslator);

This way Qt will translate what it already knows (like it's own widgets) and will use the custom translations provided with the application for the rest.

Shirkrin
  • 3,993
  • 1
  • 29
  • 35
  • But,it doesn't work like following:QApplication a(argc, argv); QFileDialog filedialog(NULL,Qt::Dialog); QTranslator qtTranslator; qtTranslator.load("qt_" + QLocale::system().name(),QLibraryInfo::location(QLibraryInfo::TranslationsPath)); qDebug()<<"qt_"< – mac.ma May 28 '12 at 04:03
  • This is generally good advice. Though I don't see how this should change the "accepted" status of my answer - if the system-translated version of Open is acceptable, then using Qt's translations is preferred. If you want to replace it with a different, custom label, then you'll need to provide your own translation. The generic answer "You'll need to install a QTranslator that translates &Open in the QFileDialog context" is valid for both cases. – laalto May 28 '12 at 08:22
  • @mac.ma - your sample works for me (e.g. launching with LC_ALL="de_DE" ./qtTest produces a German localized dialog). Are you sure the qt_*.qm files are installed correctly on your system? – Shirkrin May 29 '12 at 10:50
  • One must make sure that the translators don't go out of scope. In that case the translations are not applied. I made that mistake and wondered a while why there were no correct translations :) – juzzlin Jul 27 '19 at 18:44
1

The "Open" string is hardcoded but translated in QFileDialog:

void QFileDialogPrivate::_q_updateOkButton()
{
// ...
    if (acceptMode == QFileDialog::AcceptSave)
        button->setText(isOpenDirectory ? QFileDialog::tr("&Open") : acceptLabel);

You'll need to install a QTranslator that translates &Open in the QFileDialog context to what you want.

Also see Internationalization in Qt docs for more info.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

You can use one of the static functions in QFileDialog. Those use the native file dialog from the OS, which will be in the correct locale and translation.

Stephen Chu
  • 12,724
  • 1
  • 35
  • 46