Translation not working for QSystemTrayIcon

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Translation not working for QSystemTrayIcon

zdenko podobny
Hello,

I am not sure what I am doing wrong, but translation is not working for my QSystemTrayIcon app. Here is my testing code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class SystemTrayIcon(QtWidgets.QSystemTrayIcon):

def __init__(self, icon, parent=None):
QtWidgets.QSystemTrayIcon.__init__(self, icon, parent)
self.menu = QtWidgets.QMenu(parent)
settingsAction = self.menu.addAction(self.tr("Settings..."))
settingsAction.triggered.connect(self.settings)
exitAction = self.menu.addAction(self.tr("Exit"))
exitAction.triggered.connect(self.exit)
self.setContextMenu(self.menu)

def exit(self):
QtCore.QCoreApplication.exit()
print("Finished!")

def settings(self):
print("Not implemented yet...")


app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
trayIcon = SystemTrayIcon(QtGui.QIcon('logo.ico'), widget)
qtTranslator = QtCore.QTranslator()
appTranslator = QtCore.QTranslator(app)
loaded = appTranslator.load('sk_SK.qm')
print("Translation loaded:", loaded)
if loaded: # test translating:
_translate = appTranslator.translate
print("should NOT be 'Settings...': " +
_translate("SystemTrayIcon", "Settings..."))
trayIcon.show()
sys.exit(app.exec_())

Output in terminal is:
Translation loaded: True
should NOT be 'Settings...': Nastavenia…

So translation was loaded correctly and it is working... But when I invoke context menu on systray icon - menu is in English:
image.png

How to fix it?  
I am using PyQt5 5.11.3, Python 3.6.6 64bit on Windows 10.

Zdenko

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Translation not working for QSystemTrayIcon

Hans Jörg Maurer
Did you do the procedure with the .pro file / lupdate / pylupdate5 / QLinguist and lrelease? I use only .tr() not .translate()



Am Donnerstag, den 21.03.2019 um 09:32 schrieb Zdenko Podobny:
Hello,

I am not sure what I am doing wrong, but translation is not working for my QSystemTrayIcon app. Here is my testing code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class SystemTrayIcon(QtWidgets.QSystemTrayIcon):

def __init__(self, icon, parent=None):
QtWidgets.QSystemTrayIcon.__init__(self, icon, parent)
self.menu = QtWidgets.QMenu(parent)
settingsAction = self.menu.addAction(self.tr("Settings..."))
settingsAction.triggered.connect(self.settings)
exitAction = self.menu.addAction(self.tr("Exit"))
exitAction.triggered.connect(self.exit)
self.setContextMenu(self.menu)

def exit(self):
QtCore.QCoreApplication.exit()
print("Finished!")

def settings(self):
print("Not implemented yet...")


app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
trayIcon = SystemTrayIcon(QtGui.QIcon('logo.ico'), widget)
qtTranslator = QtCore.QTranslator()
appTranslator = QtCore.QTranslator(app)
loaded = appTranslator.load('sk_SK.qm')
print("Translation loaded:", loaded)
if loaded: # test translating:
_translate = appTranslator.translate
print("should NOT be 'Settings...': " +
_translate("SystemTrayIcon", "Settings..."))
trayIcon.show()
sys.exit(app.exec_())

Output in terminal is:
Translation loaded: True
should NOT be 'Settings...': Nastavenia…

So translation was loaded correctly and it is working... But when I invoke context menu on systray icon - menu is in English:
image.png

How to fix it?  
I am using PyQt5 5.11.3, Python 3.6.6 64bit on Windows 10.

Zdenko

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Translation not working for QSystemTrayIcon

zdenko podobny
I do not use .pro file - I run it manually:
pylupdate5 scratch_1.py -ts sk_SK.ts
linguist sk_SK.ts
lrelease sk_SK.ts

QTranslator.translate() returns the translation for the key (context, sourceText, disambiguation). In my example above it is not replaceable with QObject.tr()


Zdenko


št 21. 3. 2019 o 9:44 Hans Jörg Maurer <[hidden email]> napísal(a):
Did you do the procedure with the .pro file / lupdate / pylupdate5 / QLinguist and lrelease? I use only .tr() not .translate()



Am Donnerstag, den 21.03.2019 um 09:32 schrieb Zdenko Podobny:
Hello,

I am not sure what I am doing wrong, but translation is not working for my QSystemTrayIcon app. Here is my testing code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class SystemTrayIcon(QtWidgets.QSystemTrayIcon):

def __init__(self, icon, parent=None):
QtWidgets.QSystemTrayIcon.__init__(self, icon, parent)
self.menu = QtWidgets.QMenu(parent)
settingsAction = self.menu.addAction(self.tr("Settings..."))
settingsAction.triggered.connect(self.settings)
exitAction = self.menu.addAction(self.tr("Exit"))
exitAction.triggered.connect(self.exit)
self.setContextMenu(self.menu)

def exit(self):
QtCore.QCoreApplication.exit()
print("Finished!")

def settings(self):
print("Not implemented yet...")


app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
trayIcon = SystemTrayIcon(QtGui.QIcon('logo.ico'), widget)
qtTranslator = QtCore.QTranslator()
appTranslator = QtCore.QTranslator(app)
loaded = appTranslator.load('sk_SK.qm')
print("Translation loaded:", loaded)
if loaded: # test translating:
_translate = appTranslator.translate
print("should NOT be 'Settings...': " +
_translate("SystemTrayIcon", "Settings..."))
trayIcon.show()
sys.exit(app.exec_())

Output in terminal is:
Translation loaded: True
should NOT be 'Settings...': Nastavenia…

So translation was loaded correctly and it is working... But when I invoke context menu on systray icon - menu is in English:
image.png

How to fix it?  
I am using PyQt5 5.11.3, Python 3.6.6 64bit on Windows 10.

Zdenko

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Translation not working for QSystemTrayIcon

Dmitry Shachnev
In reply to this post by zdenko podobny
Hello,

On Thu, Mar 21, 2019 at 09:32:37AM +0100, Zdenko Podobny wrote:

> I am not sure what I am doing wrong, but translation is not working for my
> QSystemTrayIcon app. Here is my testing code:
>
> [...]
> app = QtWidgets.QApplication(sys.argv)
> widget = QtWidgets.QWidget()
> trayIcon = SystemTrayIcon(QtGui.QIcon('logo.ico'), widget)
> qtTranslator = QtCore.QTranslator()
> appTranslator = QtCore.QTranslator(app)
> loaded = appTranslator.load('sk_SK.qm')
> [...]
Try creating SystemTrayIcon after you load the translation, not before.

--
Dmitry Shachnev

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Translation not working for QSystemTrayIcon

zdenko podobny
Thanks for suggestion. I thought (based on some example) that Qt is able to change translation on the fly, so order is not imported. But it is.
+ I need to add app.installTranslator(appTranslator). Finale working example (relevant part) is:
app = QtWidgets.QApplication(sys.argv)
appTranslator = QtCore.QTranslator()
loaded = appTranslator.load('sk_SK.qm')
app.installTranslator(appTranslator)
print("Translation loaded:", loaded)
if loaded: # test translating:
_translate = appTranslator.translate
print("should NOT be 'Settings...': " +
_translate("SystemTrayIcon", "Settings..."))
widget = QtWidgets.QWidget()
trayIcon = SystemTrayIcon(QtGui.QIcon('logo.ico'), widget)
trayIcon.show()
sys.exit(app.exec_())

Zdenko


št 21. 3. 2019 o 13:18 Dmitry Shachnev <[hidden email]> napísal(a):
Hello,

On Thu, Mar 21, 2019 at 09:32:37AM +0100, Zdenko Podobny wrote:
> I am not sure what I am doing wrong, but translation is not working for my
> QSystemTrayIcon app. Here is my testing code:
>
> [...]
> app = QtWidgets.QApplication(sys.argv)
> widget = QtWidgets.QWidget()
> trayIcon = SystemTrayIcon(QtGui.QIcon('logo.ico'), widget)
> qtTranslator = QtCore.QTranslator()
> appTranslator = QtCore.QTranslator(app)
> loaded = appTranslator.load('sk_SK.qm')
> [...]

Try creating SystemTrayIcon after you load the translation, not before.

--
Dmitry Shachnev
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt