In Python 2 such translate function returns str object (a sequence of bytes) and it can be after converted to unicode by using .decode('utf-8') attribute. And this is the problematic part as in Python 3 str object doesn't have .decode('utf-8') attribute anymore. As Python 3 str represents a unicode string by default. Therefore wherever .decode('utf-8') attribute is used for a string in the code ATM that needs to be made compatible with both Python 2 and Python 3. That is what we talked about on how to achieve that.
But if we go a step back. What would stop working if translate function would return unicode object by default? As using:
Code: Select all
.encode('utf-8").decode('utf-8')
Code: Select all
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def translate(context, text):
"convenience function for Qt translator"
return QtGui.QApplication.translate(context, text, None, _encoding)
except AttributeError:
def translate(context, text):
"convenience function for Qt translator"
return QtGui.QApplication.translate(context, text, None)
P.S. I guess we could just continue to return unicode as DraftGui translate function does. And therefore we don't need to use .decode('utf-8') attribute anymore on str object when using Python 2. And by not using .decode('utf-8') attribute on str object we get Python 3 compatible code.