[PyQt5]Problem with translation using tr or translate



  • I have a problem with the translation I'm trying to implement with sertain places. The translation works in most places except on a QMessageBox(also on a notify2 implemetation I used before that) and on Tooltip in QSystemTrayIcon.
    I've searched in every place I found for qt and pyqt related forums etc, I've tried many different appraches based on info I found, I've update and recreate .ts/.qm file many times but nothing works.
    I have installed the translator like this:

    def run():
        app = QtWidgets.QApplication(sys.argv)
        app.setStyle("adwaita")
        app.setQuitOnLastWindowClosed(False)
    
        defaultLocale = QLocale.system().name()
        print(defaultLocale)
    
        translator = QTranslator(app)
        if defaultLocale == "el_GR":
            translator.load(":ui/resources/translations/el_GR")
            app.installTranslator(translator)
        app.setApplicationName("Snooze Shutdown")
        app.setApplicationDisplayName("Snooze Shutdown")
        
    
        ui = MainWindow()
        ui.retranslateUi(ui)
        ui.show()
        return app.exec_()
    

    I don't use qt' s .ts files(it doesn't exist for the language I use), but I haven't translation problems with any of the ui items, at least with self(tr), except the places I mentioned above.
    I tried also to use QCoreApplication.translate instead of self(tr), but with this I have more untranslated items.
    The problematic are:
    qmessagebox:
    ```

    def timerDialog(self):
            font = QFont()
            font.setFamily("Ubuntu")
            font.setPointSize(14)
            msg = QMessageBox()
            msg.setIconPixmap(QtGui.QPixmap(":ui/resources/images/snooze.png"))
            msg.setText(self.tr("The countdown timer finished!"))
            msg.setWindowTitle(self.tr("Notification"))
            msg.setStandardButtons(QMessageBox.Close)
            button = msg.button(QMessageBox.Close)
            button.setText(self.tr("Dismiss"))
            msg.setFont(font)
            msg.setStyleSheet("QMessageBox { background-image: url(:ui/resources/images/back.jpg) }");
            QTimer.singleShot(5000, lambda: msg.done(1))
            msg.exec()
    

    nothing get translated in this widget
    QSystemTrayIcon:

    tray_menu = QMenu()
    
            show_action = QAction(restimg, self.tr("Restore"), self)
            logout_action = QAction(logimg, self.tr("Logout now"), self)
            reb_action = QAction(rebimg, self.tr("Reboot now"), self)
            shut_action = QAction(shutimg, self.tr("Shutdown now"), self)
            susp_action = QAction(suspimg, self.tr("Suspend now"), self)
            hide_action = QAction(self.tr("Hide"), self)
            quit_action = QAction(exitimg, self.tr("Exit"), self)
            show_action.triggered.connect(self.show)
            logout_action.triggered.connect(Actions.Logoutcmd)
            reb_action.triggered.connect(Actions.Rebcmd)
            shut_action.triggered.connect(Actions.Shutcmd)
            susp_action.triggered.connect(Actions.Suspcmd)
            hide_action.triggered.connect(self.hide)
            quit_action.triggered.connect(QApplication.instance().quit)
    
            tray_menu.addAction(show_action)
            tray_menu.addSeparator()
            tray_menu.addAction(logout_action)
            tray_menu.addAction(reb_action)
            tray_menu.addAction(shut_action)
            tray_menu.addAction(susp_action)
            tray_menu.addAction(hide_action)
            tray_menu.addSeparator()
            tray_menu.addAction(quit_action)
    
            self.trayicon.activated.connect(self.on_systray_activated)
            self.trayicon.setContextMenu(tray_menu)
            self.trayicon.setToolTip(self.tr("No action have set. Right click to access the menu."))
            self.trayicon.show()
            self.snoozedlg = SnoozeDialog(self)
    

    Here only the Tooltip doesn't translated. But if I use QCoreApplication.translate() with "global" as context, then translation doesn't work for the other items also.
    And while I tried different things I also found a strange behaviour on another string:

    outs = self.tr("The") + " " + s + " " + self.tr("will perform at") + "   " + self.time2Go()
    

    The above appears all translated fine. but if remove the empty spaces and put them as parts of the words( "The ", " will perform at "), then translation doesn't work. Of course I have update the .ts files and also delete all cache files before rerun the application.

    What I'm doing wrong here?
    I had some java and android programming in the past and localization there was quite easy.
    Thanks in advance.



  • I created a test application using the same code, with just a button which shows the untranslated dialog:

    import os
    import sys
    
    from PyQt5 import QtCore, QtWidgets, QtGui
    from PyQt5.QtCore import QLocale, QTranslator, QTimer, pyqtSlot, QCoreApplication
    from PyQt5.QtGui import QFont
    from PyQt5.QtWidgets import QMainWindow, QMessageBox
    
    from mainwindow import Ui_MainWindow
    
    
    class Main(QMainWindow, Ui_MainWindow):
    
        def __init__(self, parent=None):
    
    
            """
                            Constructor
    
                            @param parent reference to the parent widget
                            @type QWidget
                            """
            super(Main, self).__init__(parent)
            self.setupUi(self)
            self.setWindowFlags(QtCore.Qt.WindowCloseButtonHint)
    
        def timerDialog(self):
            font = QFont()
            font.setFamily("Ubuntu")
            font.setPointSize(14)
            msg = QMessageBox()
            msg.setIconPixmap(QtGui.QPixmap(":ui/resources/images/snooze.png"))
            msg.setText(self.tr("The countdown timer finished!"))
            msg.setWindowTitle(self.tr("Notification"))
            msg.setStandardButtons(QMessageBox.Close)
            button = msg.button(QMessageBox.Close)
            button.setText(self.tr("Dismiss"))
            msg.setFont(font)
            msg.setStyleSheet("QMessageBox { background-image: url(:ui/resources/images/back.jpg) }");
            QTimer.singleShot(5000, lambda: msg.done(1))
            msg.exec()
    
        @pyqtSlot()
        def on_btnexit_clicked(self):
            """
                    Slot documentation goes here.
                    """
            sys.exit()
    
        @pyqtSlot()
        def on_btnmsg_clicked(self):
            """
                    Slot documentation goes here.
                    """
            self.timerDialog()
    
    def run():
        app = QtWidgets.QApplication(sys.argv)
        app.setStyle("adwaita")
        app.setQuitOnLastWindowClosed(False)
    
        defaultLocale = QLocale.system().name()
        print(defaultLocale)
    
        translator = QTranslator(app)
    
        LOCAL_DIR = os.path.dirname(os.path.realpath(__file__))
        print(LOCAL_DIR)
    
    
        if defaultLocale == "el_GR":
            #translator.load(":translations/el_GR")
            translator.load(LOCAL_DIR + "/translations/el_GR.qm")
            app.installTranslator(translator)
        app.setApplicationName("Snooze Shutdown")
        app.setApplicationDisplayName("Snooze Shutdown")
    
        ui = Main()
        ui.retranslateUi(ui)
        ui.show()
        return app.exec_()
    

    As I said I did it the same way with copy/paste the parts from the problematic project.
    Used the same way to create/update the translation file, and I run the test app. Translation now works for all items!
    So, now I really don't know what to do.



  • What is the parent of QMessageBox and SystemTrayIcon? WAG here, but wouldn't the translation only apply to direct children of MainWindow? In Your example QMessageBox has a null parent.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.