[Answer]-Is Django's localflavor.ca.ca_provinces.PROVINCE_CHOICES right?

1👍

If you closely observe the code for PROVINCE_CHOICES, it has been using:

from django.utils.translation import ugettext_lazy as _

PROVINCE_CHOICES = (
    ('AB', _('Alberta')),
    ('BC', _('British Columbia')),
    ('MB', _('Manitoba')),

Now, ugettext_lazy:

The result of a ugettext_lazy() call can be used wherever you would
use a unicode string (an object with type unicode) in Python. If you
try to use it where a bytestring (a str object) is expected, things
will not work as expected, since a ugettext_lazy() object doesn’t know
how to convert itself to a bytestring.

Now, observe:

If you ever see output that looks like "hello
<django.utils.functional...>", you have tried to insert the result of
ugettext_lazy() into a bytestring. That’s a bug in your code.

If I am not wrong, the django-localflavor has ignored ugettext_lazy can be used where you use a Unicode object.

If you really want to continue with ugettext_lazy for some reason:

ugettext_lazy("Hello").encode('utf-8') — Output: 'Hello'

or I could also get the unicode of the lazy proxy to get evaluated:

unicode(ugettext_lazy(u"Hello")) — Output : u'Hello'

NOTE: ugettext_lazy call is evaluated before the proper locale has been set.

Interesting reading on ugettext_lazy

Leave a comment