[Sugar-devel] Unicode strings in translations

Manuel Kaufmann humitos at gmail.com
Wed Aug 15 19:12:08 EDT 2012


On Wed, Aug 15, 2012 at 10:40 AM, Martin Langhoff
<martin.langhoff at gmail.com> wrote:
>>>>> len("camión")
>> 7
>
> CAREFUL HERE. You don't understand what is happening -- it is not as
> simple as you think it is.
>
> When you say  len("camión"), you are writing that from a terminal
> (Gnome's Terminal, Sugar Terminal, xterm) that is set to use utf-8.

I don't agree with you on this point, but maybe I'm wrong.

Take a look at this. Following what I understood from your email, if I
set the encoding inside the Python script I won't have this problem,
right?

[humitos at michifus ~]$ cat test.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

s = 'camión'

print 'UTF-8, set by the first line in the script'
print 'len:', len(s)
print 'last but one letter:', s[-2]
print 'second to last:', s[-3]

print

s = u'camión'
print 'Unicode'
print 'len:', len(s)
print 'last but one letter:', s[-2]
print 'second to last:', s[-3]

[humitos at michifus ~]$ python test.py
UTF-8, set by the first line in the script
len: 7
last but one letter: �
second to last:

Unicode
len: 6
last but one letter: ó
second to last: i
[humitos at michifus ~]$

Changing the default encoding, as you said, is dangerous because we
will be hiding the problem instead of resolving it. gtk people say
this on the comments in the ticket that manuq reported.

So, the right way to handle and manipulate strings is using Unicode,
with the "u" at the beginning of each string. That is the recommended
way to do it. This is not me, I mean, it is not a whim. There are many
reasons to use Unicode.

More information that you can read, pros and cons:

 * http://www.shutupandship.com/2012/01/working-with-text-in-python-use-unicode.html
 * http://stackoverflow.com/questions/1116449/should-i-use-unicode-string-by-default
 * http://blog.ianbicking.org/illusive-setdefaultencoding.html
 * http://nedbatchelder.com/blog/200401/printing_unicode_from_python.html
 * http://getpython3.com/diveintopython3/strings.html#one-ring-to-rule-them-all
 * http://blog.notdot.net/2010/07/Getting-unicode-right-in-Python

See you around,

-- 
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