简体   繁体   中英

QFileDialog localization

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 ?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM