Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#16449 closed Bug (worksforme)

i18n_pattern URLs reversed from shell and tests have incorrect language prefix

Reported by: jakub Owned by: nobody
Component: Translations Version: master
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


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:

$ ./ shell
>>> from django.conf import settings
>>> settings.LANGUAGE_CODE

>>> from django.utils import translation
>>> translation.get_language()

>>> from django.core.urlresolvers import reverse
>>> reverse('test')

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:

$ ./ 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 4 years ago.

Download all attachments as: .zip

Change History (4)

comment:1 Changed 4 years ago by jezdez

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

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 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 Changed 4 years ago by jakub

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 Changed 3 years ago by aigarius

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