PySide: translate() String with variable arguments
-
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.
-
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?
-
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:
@# -- coding: utf-8 --
import sys
from PySide import QtCore, QtGuidef 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>@