Code

Opened 14 months ago

Closed 13 months ago

Last modified 7 months ago

#19811 closed Bug (fixed)

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

Reported by: lwarx Owned by: nobody
Component: Internationalization Version: master
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 lwarx 14 months ago.
example project to reproduce the problem

Download all attachments as: .zip

Change History (14)

Changed 14 months ago by lwarx

example project to reproduce the problem

comment:1 follow-up: Changed 14 months ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.5-rc-1 to master

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

comment:2 Changed 14 months ago by lwarx

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

comment:3 in reply to: ↑ 1 Changed 14 months ago by konradhalas

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 Changed 14 months ago by konradhalas

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

comment:5 Changed 14 months ago by aaugustin

  • Has patch set

comment:6 Changed 14 months ago by Konrad Hałas <halas.konrad@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 6bd6a568960e9ddebd78da8e008b235b3b76c5a2:

Fixed #19811 - Added language code fallback in get_language_info.

comment:7 Changed 14 months ago by Honza Král <Honza.Kral@…>

In 0ad76843b5032af52022adff7b676db633ce8b7e:

Merge pull request #777 from khalas/ticket_19811

Fixed #19811 - Added language code fallback in get_language_info

comment:8 Changed 13 months ago by nigma

  • Resolution fixed deleted
  • Status changed from closed to new

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 Changed 13 months ago by lwarx

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 Changed 13 months ago by lwarx

  • Resolution set to fixed
  • Status changed from new to closed

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

comment:11 Changed 11 months ago by jorge.cadena@…

language problem in some browsers

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

django = 1.5.1

comment:12 Changed 9 months ago by anonymous

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 Changed 7 months ago by steve@…

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'

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.