ghostmansd
ghostmansd

Reputation: 3455

PyQt4: QFileDialog and QFontDialog localization

Subj: is it possible? For example, can I translate QtGui.QFileDialog().getSaveFileName() button "Save" to "Conservare", and "Cancel" to "Ignorare"? Is it possible to create my class based on QFileDialog/QFontDialog without inventing a velocity? Someone said that these functions will be always translated, depending os system locale. Don't believe, my Russian version of OpenSUSE says that it is a lie. :-) And Russian Windows 7 has a such behaviour. All strings which I see on my systems are English. I'm not a nationalist, but I want to use strings in other languages. :-) Thanks!

Upvotes: 0

Views: 1380

Answers (2)

ekhumoro
ekhumoro

Reputation: 120578

A standard Qt installation should include 20 or so translation files for the Qt library itself. These files can be found at runtime by using QLocale and QLibraryInfo to construct the arguments used by QTranslator.load. Further details can be found in this section of the Qt i18n docs.

Here's a basic PyQt4 example:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.buttons = QtGui.QDialogButtonBox(self)
        button = self.buttons.addButton(QtGui.QDialogButtonBox.Open)
        button.clicked.connect(self.handleOpen)
        button = self.buttons.addButton(QtGui.QDialogButtonBox.Close)
        button.clicked.connect(self.close)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.buttons)

    def handleOpen(self):
        dialog = QtGui.QFileDialog()
        dialog.exec_()

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    translator = QtCore.QTranslator()
    if len(sys.argv) > 1:
        locale = sys.argv[1]
    else:
        locale = QtCore.QLocale.system().name()
    name = 'qt_%s' % locale
    path = QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.TranslationsPath)
    if translator.load(name, path):
        app.installTranslator(translator)
    else:
        print('ERROR: could not load translation file:'
              ' name=%r, path=%r' % (name, path))
    window = Window()
    window.show()
    sys.exit(app.exec_())

Upvotes: 2

ghostmansd
ghostmansd

Reputation: 3455

I've already found a solution: qm files. You can get them from ts files, using lrelease.

Upvotes: 0

Related Questions