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

pyside tr() does not support inheritance

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4: Low
    • Resolution: Unresolved
    • Affects Version/s: 1.1.0, dev
    • Fix Version/s: dev
    • Component/s: PySide
    • Environment:
      Ubuntu 11.04 64-bit

      Description

      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
                      translator.
          @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__
                  break
      
          # 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 )
          else:
              return qApp.translate ( context, msg )
      
      

        Attachments

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

          Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              nmelchior Nik Melchior
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:

                  Gerrit Reviews

                  There are no open Gerrit changes