[Sugar-devel] Unicode strings in translations

Manuel Kaufmann humitos at gmail.com
Mon Aug 13 12:35:51 EDT 2012


Hello,

I'm working on Typing Turtle Gtk3 port and I found an error with the
translations encoding. The thing is we can't combine Unicode strings
and 8-bits strings:


>>> '¡Hola %s!' % 'camión'
'\xc2\xa1Hola cami\xc3\xb3n!'

>>> u'¡Hola %s!' % u'camión'
u'\xa1Hola cami\xf3n!'

>>> '¡Hola %s!' % u'camión'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position
0: ordinal not in range(128)

>>> u'¡Hola %s!' % 'camión'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
4: ordinal not in range(128)


In Typing Turle the _ function (from gettext import gettext as _) is
returning 8-bits strings, so it crashes when it tries to do something
like this:

_('Congratulations!  You earned a %(type)s medal!') % u'gold'
[...]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
4: ordinal not in range(128)

But, if we don't mix Unicode and 8-bits strings it's possible to replace them:

>>> _('Congratulations!  You earned a %(type)s medal!')
"Felicidades!  Has obtenido una medalla de %(type)s!"
>>> _('Congratulations!  You earned a %(type)s medal!') % {'type': 'ORO'}
"Felicidades!  Has obtenido una medalla de ORO!"

To get Unicode strings from gettext I had to put these lines in my
lesssonscreen.py file:

import gettext
gettext.install('po', unicode=True)

>>> _('Congratulations!  You earned a %(type)s medal!')
u"Felicidades!  Has obtenido una medalla de %(type)s!"
>>> _('Congratulations!  You earned a %(type)s medal!') % {'type': u'ORO'}
"Felicidades!  Has obtenido una medalla de ORO!"

So, is there a way to make this available at Sugar level? This issue
appears in many activities and it would be great to solve it
"upstream" :)

Thanks,

Reference: http://docs.python.org/library/gettext.html

-- 
Kaufmann Manuel
Blog: http://humitos.wordpress.com/
Porfolio: http://fotos.mkaufmann.com.ar/
PyAr: http://www.python.com.ar/


More information about the Sugar-devel mailing list