Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to create .ts files for Qt Linguist with PySide6?



  • Hi all,

    I have a python project written with PySide2 and now I want to migrate to PySide6.
    My UI supports different languages and I did translation with help of Qt Linguist.
    I created .ts files with help of this command:
    pylupdate5 -noobsolete -translate-function g_tr my_project.pro
    Here g_tr is my alias for translate() function for shorter code (I haven't found better solution to type less text) defined this way:

    def g_tr(context, text):
        return QCoreApplication.translate(context, text)
    

    and pylupdate5 is actually not from PySide2 but from PyQt5. I don't know why I took it long time ago but it is still there and does the job. But I think it's no good as I plan to get rid of PySide2 and PyQt5 packages.

    So, I need to replace pylupdate5 with something from PySide6 package. I assumed lupdate should to the trick but it seems it works only with C++ code (at least it gives me errors like Unterminated C++ character or Unbalanced opening parenthesis in C++ code. An with lupdate -help I don't see how I may switch it to python mode (similar to uic -g python).

    So, could you please give me advice - how should I create .ts files from my python sources?


  • Lifetime Qt Champion

    Hi,

    I think you hit that roadblock.





  • Thanks a lot, guys.
    You are right - I used outdated lupdate. I upgraded qt6-tools package and got a new one from Qt 6.2. It appears I need to tweak command line options but at least I don't have C++ related errors anymore.
    I'll finalize my command line, post it here and then close the topic as resolved.



  • I played a bit with translation and it appears tr-function-alias option doesn't work.
    I invoked lupdate with this command

    /usr/lib/qt6/bin/lupdate -no-obsolete my.pro
    

    and here is my pro-file, very simple (I have a couple of examples with Qt 5.15 and 6.2 - they work. So I'm sure it is ok):

    SOURCES = ./main.py ./window.py
    TRANSLATIONS = ./en.ts ./ru.ts
    

    If my source code contain calls to self.tr() like

    self.button.setText(self.tr("Check state"))
    

    then everything is ok, I get below output and ts-files have expected strings for translation:

    Updating 'en.ts'...
        Found 1 source text(s) (1 new and 0 already existing)
    Updating 'ru.ts'...
        Found 1 source text(s) (1 new and 0 already existing)
    

    But if I use this kind of code with custom translation function:

    def custom_tr(text):
        return QApplication.translate("Context", text)
       . . . . . . . . . . 
    
        self.button.setText(custom_tr("Check state"))
    

    and invoke lupdate with command

    /usr/lib/qt6/bin/lupdate -no-obsolete -tr-function-alias tr=custom_tr my.pro
    

    then it doesn't find strings to translate:

    Updating 'en.ts'...
        Found 0 source text(s) (0 new and 0 already existing)
    Updating 'ru.ts'...
        Found 0 source text(s) (0 new and 0 already existing)
    

    I used this approach with custom_tr with pylupdate5 and PySide2 - it worked fine. But option tr-function-alias for lupdate doesn't work as expected for PySide6. Either I don't understand syntax (but I played around and can't find other options) or somethign is wrong with it.

    Should I report it as a bug somehow?...


  • Lifetime Qt Champion

    Unless the removal was documented, then I would say yes, it's a bug.



  • @SGaist said in How to create .ts files for Qt Linguist with PySide6?:

    Unless the removal was documented, then I would say yes, it's a bug.

    lupdate --help shows this option as available with detailed usage explanation.
    So I would vote for a bug. But I have no idea how to report it (have never done it before for Qt project). Any help/advices?



  • @StarterKit You must create a bug in https://bugreports.qt.io/.




Log in to reply