Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. PySide: translate() String with variable arguments

PySide: translate() String with variable arguments

Scheduled Pinned Locked Moved Language Bindings
4 Posts 2 Posters 8.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pbouda
    wrote on 13 May 2011, 11:45 last edited by
    #1

    Hi,

    In C++ and PyQt it is possible to use the arg() method on QStrings to insert some variables, so it was possible to translate those variable strings, for example:

    @self.labelHalloWelt = QtGui.QLabel(self.tr("Hello %1!").arg("World"))@

    I can now translate "Hello %1!" to "Hallo %1!" in the Linguist. How do I do this when I use PySide? It tried to use Python's format() like this:

    @self.labelHalloWelt = QtGui.QLabel(self.tr("Hello {0}!".format("World")))@

    This does not work, if I translate to "Hallo {0}!" I still get the English string.

    Peter

    http://www.peterbouda.eu

    1 Reply Last reply
    0
    • R Offline
      R Offline
      renato.filho
      wrote on 13 May 2011, 13:50 last edited by
      #2

      I have created a samall example here and everything works fine, these are my files:

      trans_latin.ts

      @
      <?xml version="1.0" encoding="utf-8"?>
      <!DOCTYPE TS><TS version="1.1" language="lt">
      <defaultcodec></defaultcodec>
      <context>
      <name>QObject</name>
      <message>
      <source>Hello, {0}!</source>
      <translation>Orbis, {0}!</translation>
      </message>
      </context>
      </TS>
      @

      test.py:
      @
      translator = QTranslator()
      translator.load(os.path.join(self.trdir, 'trans_latin.qm'))
      self.app.installTranslator(translator)

          obj = QObject()
          obj.setObjectName(obj.tr('Hello, {0}!'))
          self.assertEqual(obj.objectName(), u'Orbis, {0}!')
      

      @

      Can you give me a complete example?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pbouda
        wrote on 16 May 2011, 08:58 last edited by
        #3

        Your example throws an Assertion Error at me...

        @AssertionError: u'Hello, {0}!' != u'Orbis, {0}!'@

        (Update: sorry, my fault, your code is correct: it is not throwing any error now)

        But: did you try this with a "format()" call? As soon as I add the format() call to your code, the translation is not correct any more. How do I add the variable part to a string then?

        Here is my code:

        lokalisierung3.py:

        @# -- coding: utf-8 --

        import sys
        from PySide import QtCore, QtGui

        def main(argv):
        app = QtGui.QApplication(argv)

        QtGui.QApplication.setApplicationName("HalloWelt")
        
        myappTranslator = QtCore.QTranslator()
        myappTranslator.load("lokalisierung3_de")
        app.installTranslator(myappTranslator)
        
        mainwindow = MyMainWindow()
        mainwindow.show()
        sys.exit(app.exec_())
        

        class MyMainWindow(QtGui.QMainWindow):

        def __init__(self, *args):
            QtGui.QMainWindow.__init__(self, *args)
            self.createComponents()
            self.createLayout()
        
            self.setWindowTitle(self.tr("Hello World"))
            
        def createComponents(self):
            self.labelHalloWelt = QtGui.QLabel(self.tr("Hello {0}!".format("World")))
            self.buttonTextAktualisieren = QtGui.QPushButton(self.tr("Update"))
            self.editText = QtGui.QLineEdit()
        
        def createLayout(self):
            layoutZentral = QtGui.QVBoxLayout()
            layoutZentral.addWidget(self.labelHalloWelt)
            layoutZentral.addWidget(self.editText)
            layoutZentral.addWidget(self.buttonTextAktualisieren)
            
            widgetZentral = QtGui.QWidget()
            widgetZentral.setLayout(layoutZentral)
            self.setCentralWidget(widgetZentral)
        

        if name == "main":
        main(sys.argv)@

        lokalisierung3_de.ts:

        @<?xml version="1.0" encoding="utf-8"?>
        <!DOCTYPE TS>
        <TS version="2.0" language="de_DE">
        <context>
        <name>MyMainWindow</name>
        <message>
        <location filename="lokalisierung3.py" line="26"/>
        <source>Hello World</source>
        <translation>Hallo Welt</translation>
        </message>
        <message>
        <location filename="lokalisierung3.py" line="29"/>
        <source>Hello {0}!</source>
        <translation>Hallo {0}!</translation>
        </message>
        <message>
        <location filename="lokalisierung3.py" line="30"/>
        <source>Update</source>
        <translation>Aktualisieren</translation>
        </message>
        </context>
        </TS>@

        Peter

        http://www.peterbouda.eu

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pbouda
          wrote on 16 May 2011, 09:40 last edited by
          #4

          OK, another sorry, I found my error. If I put the format() after the tr() everything works fine:

          @self.labelHalloWelt = QtGui.QLabel(self.tr("Hello {0}!").format("World"))@

          Peter

          http://www.peterbouda.eu

          1 Reply Last reply
          0

          1/4

          13 May 2011, 11:45

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved