[PyKDE] QObject.trUtf8 doesn't accept unicode strings.

Giovanni Bajo rasky at develer.com
Wed Jan 3 19:04:44 GMT 2007


Tomasz Melcer wrote:

>> 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.
>
> Just curious: what happens if I want a string to translate which
> contains any special unicode symbols, like math operators? I couldn't
> express them as 8-byte characters...

Uh? If you call trUtf8(), it means that you are passing in a UTF-8 encoded
string. With UTF-8, you can represent any codepoint in the Unicode standard,
so there is no problem with that.

>From a *pratical* point of view, you just want to:

- Mark your Python files as UTF-8 (using the standard encoding declaration:
http://docs.python.org/ref/encodings.html).
- Use a text editor which will actually edit contents in UTF-8 form (for
instance, a Python-aware editor that will automatically adapt to the
encoding declaration of the file; or a simple text editor like vim running
with UTF-8 locale).
- Always use trUtf8(), or use tr() after having called
QTextCodec.setCodecForTr.

and that's all.
-- 
Giovanni Bajo




More information about the PyQt mailing list