[Sugar-devel] Unicode strings in translations

Daniel Narvaez dwnarvaez at gmail.com
Wed Aug 15 09:48:47 EDT 2012


I think the reason this worked in gtk2 activities is that importing
gtk had this side effect (it's in the pango module really)

    /* set the default python encoding to utf-8 */
    PyUnicode_SetDefaultEncoding("utf-8");

As you can see

>>> 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)
>>> import gtk
>>> u'¡Hola %s!' % 'camión'
u'\xa1Hola cami\xf3n!'

Now I don't really like changing default encoding as a side effect but
the simpler solution is probably to just reinstate that, either in
pygi or in sugar-toolkit. Then with python 3 hopefully stuff will work
without this kind of hacks.

On 13 August 2012 18:35, Manuel Kaufmann <humitos at gmail.com> wrote:
> 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/
> _______________________________________________
> Sugar-devel mailing list
> Sugar-devel at lists.sugarlabs.org
> http://lists.sugarlabs.org/listinfo/sugar-devel



-- 
Daniel Narvaez


More information about the Sugar-devel mailing list