Python Forum
[PyQt] Linux - Application translation not load when run from .desktop launcher
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Linux - Application translation not load when run from .desktop launcher
#1
Hello,
I've create an pyqt5 application a year ago which I'm using every day and works fine. Recently I tried to remove the hardcoded texts which are in my native language with english and add translation support. After I found some info on internet I've manage to achive what I wanted and the translation loads and works fine when I'm running the app from command line or via a script.
The problem is that when I create a .desktop file for the app or when I add it to autostart programs(in which again a .desktop file created), then the transaltion doesn't load and all texts in app appears in english language.
I incorporate the translation like this:
mainwindow.py
def run():
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("adwaita")
    app.setQuitOnLastWindowClosed(False)
    translator=QTranslator(app)
    translator.load("ui/resources/translations/el_GR.qm")
    app.installTranslator(translator)
    ui = MainWindow()
    ui.show()
    return app.exec_()
main.py:
from ui import mainwindow
if __name__ == "__main__":
    import sys
    sys.exit(mainwindow.run())
I've create all forms and widgets in qtdesigner and I have "translate" checked for all labels/buttons etc.
I also used self.tr() to translate all other strings.
I mentioned that when I directly run the app from command line or script, the preferences file I've implement created correct in app's folder, but when I run it from the .desktop file or autostarts at login, then the preferences file created in my user's home directory. So I'm thinking that this problem is related to translations problem. Seems that when it launches from the launcher file then doesn't look for the preferences file or the translation file on the app's folder and subfolders, but on user's home folder only.
Any help? Is this application's problem? Do I need to use some other way to declare the path to .qm file and also to preferences for the app? I don't think that is linux's issue, because I don't have this problems with other python/qt apps which are installed.
Reply
#2
Perhaps it cannot find the path of your script, try something like:

    LOCAL_DIR = os.path.dirname(os.path.realpath(__file__))
    translator.load(LOCAL_DIR + "/ui/resources/translations/el_GR.qm")
Reply
#3
Thanks, it works with realpath. Didn't work using the path from your post though. The mainwindow.py file is inside the "ui' folder, so the path needs to be "/resources/translations/el_GR.qm".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyQt] Application desktop toolbar with PyQt6 bunz 4 1,451 Mar-09-2023, 08:09 PM
Last Post: bunz
  [Kivy] Kivy UI language translation lib mikepy 2 1,107 Feb-11-2023, 02:12 PM
Last Post: mikepy
  pythonscript for translation into Tkinter input 3 2,141 Aug-21-2020, 09:13 AM
Last Post: input
  Module/library recommendation GUI app launcher croyleje 0 1,732 Nov-20-2019, 08:05 PM
Last Post: croyleje
  video player inside a frame/panel in python raspberry pi desktop application MATHEWS 1 2,812 Dec-12-2018, 11:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020