Take the 2-minute tour ×

I have simple app with English and Hungarian language. All localizables are in separate modul -localizable.py, for example:

# -*- coding: utf-8 -*-
if language == "hun":
    LOGIN_LABEL_USERNAME_STR                    = 'Felhasználó'
    LOGIN_LABEL_PASSWORD_STR                    = 'Jelszó'
elif language == "eng":    
    LOGIN_LABEL_USERNAME_STR                    = 'Username'
    LOGIN_LABEL_PASSWORD_STR                    = 'Password'

But when I use Hungerian language in loginDialog.py:

import localizable
loginLayout.addRow(localizable.LOGIN_LABEL_USERNAME_STR, QtGui.QLineEdit())
loginLayout.addRow(localizable.LOGIN_LABEL_PASSWORD_STR, QtGui.QLineEdit())

I get FelhasznÃ¡lÃ³ and JelszÃ³ instead Felhasználó and Jelszó. Any help would be appreciated.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Why not use unicode objects in your localizable module, instead of bytestrings with no encoding?

if language == "hun":
    LOGIN_LABEL_USERNAME_STR                    = u'Felhasználó'
    LOGIN_LABEL_PASSWORD_STR                    = u'Jelszó'

This has the intended effect for me.

You also might want to consider using the built-in functions for this provided by Qt.

share|improve this answer
Tnx, that's it, but I've left coding utf-8 because without it I get this SyntaxError: Non-ASCII character '\xc3' in file C:~\localizable.py on line 49, but no encoding declared –  Aleksandar Jan 9 '13 at 14:56
@Aleksandar: You need to both specify the source encoding (in python 2; py3k assumes UTF-8) and mark them as Unicode strings (also in python 2; py3k strings are unicode rather than bytestrings). –  qwe Jan 9 '13 at 15:23
Yes I'm using python 2.6, thank you. –  Aleksandar Jan 9 '13 at 15:26
add comment

This happens because your source file is UTF8-encoded, meaning the string Felhasználó is represented as the bytes 46 65 6c 68 61 73 7a 6e c3 a1 6c c3 b3 in the source code file. If you print this byte array out as if they were in LATIN-1-encoding, you will get FelhasznÃ¡lÃ³.

Further, you pass these UTF-8 encoded bytes along as just a plain string to Qt, which seems to assume it is in LATIN-1 encoding. This means instead of assuming the bytes c3 b3 (Ã³) are code for ó as it would be in UTF8, it assumes c3 is plain Ã and b3 is ³ which is the case for LATIN1.

frb's answer is the correct way to do this, because by sending a unicode string instead, you have done all the interpretation and leave no ambiguity for Qt.

share|improve this answer
Thanks for the explanation! –  Aleksandar Jan 9 '13 at 14:57
add comment

Your Answer


