Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#19811 closed Bug (fixed)

KeyError: u"Unknown language code 'en-us'."

Reported by: Max Arnold Owned by: nobody
Component: Internationalization Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Default LANGUAGE_CODE='en-us' is not present in django.conf.locale.LANG_INFO and can trigger KeyError exception. It can be easily reproduced by using get_language_info in templates. Example project is attached.

Command to reproduce an exception:

curl -v -L http://localhost:8000

Traceback is below:

Internal Server Error: /en-us/
Traceback (most recent call last):
  File "/Users/lwarx/.virtualenvs/locale/lib/python2.7/site-packages/django/core/handlers/base.py", line 140, in get_response
    response = response.render()
  File "/Users/lwarx/.virtualenvs/locale/lib/python2.7/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/Users/lwarx/.virtualenvs/locale/lib/python2.7/site-packages/django/template/response.py", line 82, in rendered_content
    content = template.render(context)
  File "/Users/lwarx/.virtualenvs/locale/lib/python2.7/site-packages/django/template/base.py", line 140, in render
    return self._render(context)
  File "/Users/lwarx/.virtualenvs/locale/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  File "/Users/lwarx/.virtualenvs/locale/lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "/Users/lwarx/.virtualenvs/locale/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "/Users/lwarx/.virtualenvs/locale/lib/python2.7/site-packages/django/templatetags/i18n.py", line 32, in render
    context[self.variable] = translation.get_language_info(lang_code)
  File "/Users/lwarx/.virtualenvs/locale/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 150, in get_language_info
    raise KeyError("Unknown language code %r." % lang_code)
KeyError: u"Unknown language code 'en-us'."

Django version was installed from GitHub 1.5.x branch (77f6eb2a6099).

Attachments (1)

example.zip (5.8 KB ) - added by Max Arnold 12 years ago.
example project to reproduce the problem

Download all attachments as: .zip

Change History (14)

by Max Arnold, 12 years ago

Attachment: example.zip added

example project to reproduce the problem

comment:1 by Claude Paroz, 12 years ago

Triage Stage: UnreviewedAccepted
Version: 1.5-rc-1master

I also think that we should fallback to the language code stripped from the country code in this case.

comment:2 by Max Arnold, 12 years ago

The bug exists not only in Django-1.5rc1, but also Django-1.4.3.

in reply to:  1 comment:3 by konradhalas, 12 years ago

Replying to claudep:

I also think that we should fallback to the language code stripped from the country code in this case.

So in this case we should check for 'en' entry in django.conf.locale.LANG_INFO? If yes, I can provide patch with tests.

comment:4 by konradhalas, 12 years ago

I added fallback in this pull request https://github.com/django/django/pull/777

comment:5 by Aymeric Augustin, 12 years ago

Has patch: set

comment:6 by Konrad Hałas <halas.konrad@…>, 12 years ago

Resolution: fixed
Status: newclosed

In 6bd6a568960e9ddebd78da8e008b235b3b76c5a2:

Fixed #19811 - Added language code fallback in get_language_info.

comment:7 by Honza Král <Honza.Kral@…>, 12 years ago

In 0ad76843b5032af52022adff7b676db633ce8b7e:

Merge pull request #777 from khalas/ticket_19811

Fixed #19811 - Added language code fallback in get_language_info

comment:8 by nigma, 12 years ago

Resolution: fixed
Status: closednew

The ticket has been marked as fixed, but the patch has not been applied to the 1.5.x branch. Is there any chance this can be backported to 1.5?

comment:9 by Max Arnold, 12 years ago

I hoped it would be included into 1.5 release... Is it possible to backport this fix to 1.5.x branch for next minor release?

comment:10 by Max Arnold, 12 years ago

Resolution: fixed
Status: newclosed

It looks like patch was superseded by 8c8f94fe9dcdd4983ebeb59951e6f7399bc287a2 in ticket #19763

comment:11 by jorge.cadena@…, 12 years ago

language problem in some browsers

u"Unknown language code 'es-co'."

django = 1.5.1

comment:12 by anonymous, 11 years ago

u"Unknown language code 'en-us'."

django = 1.5.1
Works, when 'en-us' is replaced by just 'en'.

settings.py:

LANGUAGES = (
    ('en-us', ugettext('English')),
    ('uk', ugettext('Ukrainian')),
)

comment:13 by steve@…, 11 years ago

Am still getting this in 1.5.3, too:

LANGUAGES = (
    # See http://www.i18nguy.com/unicode/language-identifiers.html, but all lower case
    ('en-gb', _fake_gettext('UK English')),
    ('en-us', _fake_gettext('US English')),
)

LANGUAGE_CODE = 'en-gb'
Note: See TracTickets for help on using tickets.
Back to Top