Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-131

pyside tr() does not support inheritance



    • Bug
    • Resolution: Fixed
    • P4: Low
    • 5.15.4, 6.0
    • 1.1.0
    • PySide
    • Ubuntu 11.04 64-bit
    • 5054038dd43fbb243e2c44b0a9aad35181b043b1 (pyside/tqtc-pyside-setup/5.15) 5c9d4986cdc6ebf63efa0e94430798c81c2dff3e (pyside/pyside-setup/6.1) c61141d43fd6e56a52449cbfe6cc13d07f9156bb (pyside/pyside-setup/6.2)


      Perhaps this is a feature request rather than a bug, but I expected this to work. Strings are not translated if the runtime type of the object is not the same as the type in which the string appears. The simple test case attached illustrates the issue. Run

      lrelease hu_HU.ts
      python ./example.py

      Note that the source text is used, rather than the translation provided in the attached TS file.

      As a workaround, I currently use the following method:

      def tr(self, msg, arg2=None, arg3=None):
          @brief Reimplement Qt's tr() function so translations can be found for
                 strings defined in a base class, but displayed by an instance of
                 a derived class.
          @param msg  The sourceText to translate.
          @param arg2 The optional disambiguation text, or a comment to the
          @param arg3 The optional number to use for determining a plural form
                      of the translation.
          # The import is here just to keep all aspects of this hack inside the
          # function.  We need the name of the calling function in order to
          # determine which superclass actually implements it.
          import inspect
          caller = inspect.stack()[1][3]
          context = None
          for klass in self.__class__.mro():
              if caller in klass.__dict__:
                  context = klass.__name__
          # Call the application translation function.  There doesn't seem to be
          # a way to pass the context string to QObject's tr(); it is always
          # extracted from the name of self's type.  The QCoreApplication
          # translate() adds an extra argument, and I don't trust the PySide
          # docs' specification of default arguments, so I'm being very explicit
          # in passing only the arguments passed to this function.
          qApp = QCoreApplication.instance()
          if arg3:
              return qApp.translate ( context, msg, arg2, n=arg3 )
          elif arg2:
              return qApp.translate ( context, msg, arg2 )
              return qApp.translate ( context, msg )


        1. example.py
          0.7 kB
        2. hu_HU.ts
          0.5 kB

        Issue Links

          For Gerrit Dashboard: PYSIDE-131
          # Subject Branch Project Status CR V



              ctismer Christian Tismer
              nmelchior Nik Melchior
              1 Vote for this issue
              5 Start watching this issue



                Gerrit Reviews

                  There are no open Gerrit changes