Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#16449 closed Bug (worksforme)

i18n_pattern URLs reversed from shell and tests have incorrect language prefix

Reported by: Jakub Roztočil Owned by: nobody
Component: Translations Version: dev
Severity: Normal Keywords: urlconf, reverse, i18n_patterns, tests, shell
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I've been trying out the new i18n_patterns from trunk and have run into the following issue:

The django.core.urlresolvers.reverse() function always returns /en-us as the language prefix for i18n URLs when it is called from a shell session or tests. I don't have en-us in my settings.LANGUAGES so requests to such URLs result in 404s.

After some digging I've found out that the above is probably just a symptom of another issue which is that the language specified in settings.LANGUAGE_CODE isn't automatically activated for shell sessions and when running tests:

$ ./manage.py shell
>>> from django.conf import settings
>>> settings.LANGUAGE_CODE
    'es'

>>> from django.utils import translation
>>> translation.get_language()
    'en-us'

>>> from django.core.urlresolvers import reverse
>>> reverse('test')
    '/en-us/'

I've also tried adding en-us as settings.LANGUAGE_CODE and to settings.LANGUAGES, after which I was able to run my tests, but there was another issue: the get_language_info template tag raises an exception when the language code is en-us, because it isn't defined in django.conf.locale.LANG_INFO:

$ ./manage.py shell
>>> from django.utils import translation
>>> translation.get_language_info('en-us')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)

django/utils/translation/__init__.pyc in get_language_info(lang_code)
    165     from django.conf.locale import LANG_INFO
    166     try:
    167         return LANG_INFO[lang_code]
    168     except KeyError:
--> 169         raise KeyError("Unknown language code %r." % lang_code)

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

Attachments (1)

language-code-in-commands-note-ticket-16449.patch (844 bytes ) - added by Jakub Roztočil 13 years ago.

Download all attachments as: .zip

Change History (4)

comment:1 by Jannis Leidel, 13 years ago

Resolution: worksforme
Status: newclosed

That has something to do with how the base management command sets the current language to en-us by default, not settings.LANGUAGE_CODE.

See https://code.djangoproject.com/browser/django/trunk/django/core/management/base.py?rev=13993#L202 for the explanation.

Simply use translation.activate(settings.LANGUAGE_CODE) or similar to enable the language you need before reversing the URL you want.

comment:2 by Jakub Roztočil, 13 years ago

Okay, thanks. It's actually documented here. I was looking at the documentation for LANGUAGE_CODE and i18n in general where it isn't mentioned (patch attached).

It's just a shame that the language gets deactivated already in BaseCommand and not only for the core/contrib commands that actually do create some database content. I find this exception unintuitive/inconsistent.

comment:3 by Aigars Mahinovs, 12 years ago

I understand that some management commands need a neutral language, but it does not seem right that tests are run with a language code that is not the default language code of the project.

Note: See TracTickets for help on using tickets.
Back to Top