[PyKDE] QObject.trUtf8 doesn't accept unicode strings.
rasky at develer.com
Wed Jan 3 16:30:43 GMT 2007
Rick van Hattem wrote:
> I am aware of that, but I think it would be nice if trUtf8 would
> support it yes, it would have to be converted from unicode() to
> the escaped str() version.
> It's kind of pointless to use unicode
> internally (when needed) and not being able to use it because
> the tr functions don't support it.
I disagree. You are either:
1) Storing sequence of bytes encoded in UTF-8 within an unicode instance,
you are doing something wrong: you are abusing an object which is meant to
hold something else (specifically, an unicode instance is a sequence of
unicode codepoints). By stretching this, it would be almost the same if you
asked trUtf8() to accepts list of integers in range 0-255: it's obviously
not the way an encoded string is normally represented in Python.
2) Using unicode strings in your source code and asking Qt to translate
those (in which case, it's up to you to encode them to utf-8 or to latin-1
or to whatever coding you specified in QTextCodec.setCodecForTr, before
passing them to tr()). But *why* are you using *unicode* for literal strings
that are supposed to be translated? In fact, how do you expose such strings
in your source code in a way that pylupdate is able to extract them?
Your source code should look like this:
self.trUtf8("foo bar translate this")
Notice that the return value of tr()/trUtf8() is obviously a QString
(duck-typable and convertible to Python's unicode), so your application will
still be fully unicode. It's only the literal in the source code
(*immediatly* surrounded by the tr() call) which is a 8-byte str.
BTW: since all my sources are UTF-8, I prefer to specify it as default
encoding for tr (QTextCodec.setCodecForTr) so that I can later use the
shorter tr() everywhere in the code.
More information about the PyQt