On Wed, Jan 22, 2014 at 1:12 PM, Henrique Romano <
chro...@gmail.com> wrote:
>
> That was just an example, ugettext_* returns a functional.proxy, which
> doesn't try to render the string when you print it on the screen, but if you
> try to do it you will see the same result.
You are right.
On Wed, Jan 22, 2014 at 1:58 PM, Henrique Romano <
chro...@gmail.com> wrote:
>
> On Wed, Jan 22, 2014 at 2:38 PM, Shai Berger <
sh...@platonix.com> wrote:
>> You might want to take this up with django-cms.
>
> Not exactly true. Even though my problem was with django-cms, if you grep django source code for "LANGUAGES" you will find this templatetag:
>
> # templatetags/i18n.py
> 15 class GetAvailableLanguagesNode(Node):
> 16 def __init__(self, variable):
> 17 self.variable = variable
> 18
> 19 def render(self, context):
> 20 from django.conf import settings
> 21 context[self.variable] = [(k, translation.ugettext(v)) for k, v in settings.LANGUAGES]
> 22 return ''
>
> It is also translating the given language name.
Yes, I think that what both pieces of code are doing there is assume
the LANGUAGES setting in effect is the one shipped with Django where:
- All the original language names are in English (covered by ASCII)
- Most of them are translated by the Django translators and shipped
with Django (
https://github.com/django/django/blob/master/django/conf/locale/en/LC_MESSAGES/django.po#L16)
so translations are readily available.
So it's relying on the fact there there is no need to go outside ASCII
when working with original language names.
I think you are onto something re: the fact that we don't make clear
that our ugettext*() functions fail to accept encoded literals with
characters outside ASCII under Python 2.x. even when the encoding
metadata is correct.
My theory is that the facts that documentation is a bit unclear about
the requirements these source strings arguments should comply with (or
rather, what are the data types supported by these functions) and that
using another language than English as translate-from language doesn't
seem like a common setup have contributed to this not getting too much
visibility until now.
In Python 3 (3.3) this problem doesn seem to exist. See tests below.
> My suggestion is to update the documentation saying that the developer should use ugettext_lazy for the language name if they want it translated
We have something along these lines. See (last two bullets)
https://docs.djangoproject.com/en/1.6/topics/i18n/translation/#how-django-discovers-language-preference
> *or* at least use a unicode string.
I will try to get some feedback from some more experienced devs to if
there is anything to be done about this in code and/or docs.
$ cat a.py
# -*- coding: utf-8 -*-
import sys
from django.utils.translation import ugettext, ugettext_lazy, ugettext_noop
print("Python version: %s" % sys.version)
a = ugettext_noop("Portugués")
print(a)
print(type(a))
b = u'%s' % a # try to get the lazy placeholder to evaluate itself.
print(b)
print(type(b))
$ file a.py
a.py: Python script, UTF-8 Unicode text executable
PYTHONPATH=.:~/django/upstream
DJANGO_SETTINGS_MODULE=lang_i18n.settings python a.py
Python version: 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3]
Portugués
<type 'str'>
Traceback (most recent call last):
File "a.py", line 12, in <module>
b = u'%s' % a # try to get the lazy placeholder to evaluate isself.
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
7: ordinal not in range(128)
$ PYTHONPATH=.:~/django/upstream
DJANGO_SETTINGS_MODULE=lang_i18n.settings python3.3 a.py
Python version: 3.3.3 (default, Dec 27 2013, 19:27:19)
[GCC 4.6.3]
Portugués
<class 'str'>
Portugués
<class 'str'>
Thanks!
--
Ramiro Morales
@ramiromorales