Reputation: 1
I have a problem with translations which are only partial. I create my ui graphical interfaces with Qt Designer. Based on a chapter in a book on PyQt5, I have been able to translate an application in the past without using Qt Designer.
But I had never translated interfaces made with it. In fact, to be precise I have a partial translation. It concerns the strings that I introduced in the code of the application (setToolTip, setStatusTip, ....) but not the rest. I can switch between carefree english and french either without doing anything (for french) or by forcing it ex:
python start_app.py en_EN
I'm on Manjaro, python 3.8, pyqt5.14.2.
Is my code bad? Did I miss something?
I also have 3 other points to clarify regarding these translations: Is it necessary to include all the project files in order to benefit from the translation of all these? As you can see, I only put qdvgrab in my code.
when I translated my files with Qt Linguist I did not have the strings of the ui files to translate. Is this normal? Isn't the translation automatic? How to do ? Do I have to include them in my professional file and redo my translation chain (pylupdate, Qt Linguist,?)
For now, my ts, qm, pro files are at the same level as my program py files. But later, I plan to put them in a folder called locale or translation. Knowing that the file launching the application is start_app.py and it is located at the root package (see screenshot), how to import them? My python files will also be in a windows folder at that time and therefore at the same level as the others (ui, images, translation). How to import these files into start_app.py so that it can read them.
Here is the code i introduced in start_app.py
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTranslator, QLocale
from qdvgrab import QdvGrab
if __name__ == "__main__":
application = QApplication(sys.argv)
# Translate application
enNativeLanguage = len(sys.argv) == 1
if enNativeLanguage:
locale = QLocale()
# translator.load(locale, "qdvgrab", ".")
else:
languageCountry = sys.argv[1]
translators = []
for prefixQm in ("qdvgrab.", "qt_", "qtbase_"):
translator = QTranslator()
translators.append(translator)
if enNativeLanguage:
translator.load(locale, prefixQm)
else:
translator.load(prefixQm+languageCountry)
application.installTranslator(translator)
Here is the structure:
Update with minimal code to reproduce this behaviour:
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTranslator, QLocale
from minimal import MiniMal
if __name__ == "__main__":
application = QApplication(sys.argv)
# Translate application
enNativeLanguage = len(sys.argv) == 1
if enNativeLanguage:
locale = QLocale()
else:
languageCountry = sys.argv[1]
translators = []
for prefixQm in ("minimal.", "qt_", "qtbase_"):
translator = QTranslator()
translators.append(translator)
if enNativeLanguage:
translator.load(locale, prefixQm)
else:
translator.load(prefixQm+languageCountry)
application.installTranslator(translator)
window = MiniMal()
window.show()
rc = application.exec_()
sys.exit(rc)
The minimal.py code
from PyQt5.QtWidgets import QMainWindow, QMessageBox
from ui.exempleui import Ui_MainWindow
class MiniMal(QMainWindow):
def __init__(self, *args, **kwargs):
super(MiniMal, self).__init__(*args, **kwargs)
self.setupUi()
self.connectActions()
format_capture = ['Dv Raw (.dv)', 'DV 2 (.avi)', 'Dv (.avi)', 'Hdv (.m2t)', 'Mpeg 2 (.mpg)', 'Mov (.mov)']
for format in format_capture:
self.ui.cmbformatacquisition.addItem(format)
self.ui.cmbformatacquisition.setCurrentIndex(0)
def setupUi(self):
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.lnetext.setText(self.tr("My Awesome Movie"))
self.ui.btnsomething.setToolTip(self.tr("Choose the Dv Acquisition mode"))
self.ui.lnetext.setToolTip(self.tr("Set the Film Name"))
self.ui.cmbformatacquisition.setToolTip(self.tr("Choose a format"))
self.ui.chksystray.setToolTip(self.tr("Application in the systray"))
self.ui.lnetext.setStatusTip(self.tr('The Film name is '))
self.ui.btnsomething.setStatusTip(self.tr('Dv Acquisition mode is used'))
self.ui.cmbformatacquisition.setStatusTip(self.tr("Acquisition Mode Changed"))
self.ui.chksystray.setStatusTip(self.tr('Show this window in the systray'))
def connectActions(self):
self.ui.cmbformatacquisition.currentIndexChanged.connect(self.callSomething)
self.ui.lnetext.textChanged.connect(self.showName)
self.ui.chksystray.toggled.connect(self.putSystray)
self.ui.btnsomething.clicked.connect(self.callSomething)
def changeChoice(self):
pass
def putSystray(self):
pass
def showName(self):
pass
def callSomething(self):
pass
And the ui file code
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(560, 339)
MainWindow.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedKingdom))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lnetext = QtWidgets.QLineEdit(self.centralwidget)
self.lnetext.setGeometry(QtCore.QRect(210, 30, 251, 26))
self.lnetext.setObjectName("lnetext")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(36, 30, 101, 20))
self.label.setObjectName("label")
self.btnsomething = QtWidgets.QPushButton(self.centralwidget)
self.btnsomething.setGeometry(QtCore.QRect(70, 90, 361, 71))
self.btnsomething.setObjectName("btnsomething")
self.cmbformatacquisition = QtWidgets.QComboBox(self.centralwidget)
self.cmbformatacquisition.setGeometry(QtCore.QRect(80, 175, 351, 51))
self.cmbformatacquisition.setObjectName("cmbformatacquisition")
self.chksystray = QtWidgets.QCheckBox(self.centralwidget)
self.chksystray.setGeometry(QtCore.QRect(60, 250, 371, 26))
self.chksystray.setObjectName("chksystray")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 560, 28))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuHelp = QtWidgets.QMenu(self.menubar)
self.menuHelp.setObjectName("menuHelp")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionQuit = QtWidgets.QAction(MainWindow)
self.actionQuit.setObjectName("actionQuit")
self.actionAbout = QtWidgets.QAction(MainWindow)
self.actionAbout.setObjectName("actionAbout")
self.menuFile.addAction(self.actionQuit)
self.menuHelp.addAction(self.actionAbout)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuHelp.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Name"))
self.btnsomething.setText(_translate("MainWindow", "Click me"))
self.chksystray.setText(_translate("MainWindow", "Put in this window in the systray"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuHelp.setTitle(_translate("MainWindow", "Help"))
self.actionQuit.setText(_translate("MainWindow", "Quit"))
self.actionAbout.setText(_translate("MainWindow", "About"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Upvotes: 0
Views: 35