Hi,
Since 15 days, I have worked on Translations for one of my project. All was working nearly fine, I have just forgotten to translate in my pro file my ui files so I 've had a partial translation. Not a really big issue.
After including my ui files in my pro file, I’ve been able to translate then and generate my qm files like usual. I’ve decided to move them in a translation folder for reaching my final structure. Ui and Python files are in separated folder at the same level and my start_app.py file run my app without any issue. See the screenshot for project structure.
What is surrounded in red does not exist in my final structure. It was just for testing but even so it doesn’t work.
I’ve tried several combination in my start_app file but nothing until now. Initially, I was thinking that just a path problem. But Even if I have qm files at the same level than start_app file it doesn’t work.
I precise that I’m an on Linux Manjaro with Python 3.8.3, Qt 5.15.0, PyQt5 5.15.0
Now, I don’t know what doing. I’m coming completely crazy with this issue. If somebody can help me, that will be great.
PS it is a minimal example.
Here my code of my start_app.py. Like you can see,I’ve tried several things.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTranslator, QLocale, QLibraryInfo, QDir
from windows.minimal import MiniMal
if __name__ == "__main__":
application = QApplication(sys.argv)
# Translate application
# locale = QLocale.system().name()
# TranslationsPath = QDir('./translations').canonicalPath()
# translator = QTranslator()
# path = QLibraryInfo.location(QLibraryInfo.TranslationsPath)
# translator.load(locale, path)
# if len(sys.argv) == 1:
# # locale = QLocale().system().name()
# translator.load(locale, "minimal", ".")
# # translator.load('qt_%s' % locale, path)
# else:
# translator.load("minimal."+ sys.argv[1])
# # translator.load(locale + sys.argv[1])
# application.installTranslator(translator)
enNativeLanguage = len(sys.argv) == 1
if enNativeLanguage:
locale = QLocale().system().name()
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)
Here the code of the minimal.py (in the folder called windows)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
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
Here the code of the exempleui.py (in the folder called ui)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'exemple.ui'
#
# Created by: PyQt5 UI code generator 5.14.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
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_())
@martin
BTW Still an idea for the next version of the book.