Language code issue - Django thinks default is en-us?

1,504 views
Skip to first unread message

Stodge

unread,
Jun 18, 2014, 12:48:47 PM6/18/14
to django...@googlegroups.com
My settings for languages are:

LANGUAGE_CODE = 'en'

USE_I18N = True
LANGUAGE_COOKIE_NAME='django_language'
ugettext = lambda s: s
LANGUAGES = (
    ('en', ugettext('English')),
    ('de', ugettext('German')),
    ('fr', ugettext('French'))
)

I have a post-save signal that creates instances of a model and gets the current Django language. The language returned is for some reason "en-us", but I have no idea where this is coming from. My default as shown above is "en". get_language() is called from within a signal so the language isn't being taken from a cookie or request header. Any ideas why Django thinks the current (default) language is "en-us"? Thanks

Stodge

unread,
Jun 18, 2014, 1:02:37 PM6/18/14
to django...@googlegroups.com
Oh.


I forgot to mention that the signal is executed as a result of a loaddata management command. So management commands don't respect settings.LANGUAGE_CODE? That makes no sense.

Stodge

unread,
Jun 18, 2014, 1:21:25 PM6/18/14
to django...@googlegroups.com
Even when I override the language code in my custom command get_language() still returns "en-us". Weird. Guess I'll have to use settings.LANGUAGE_CODE instead of get_language().

Ilya Kazakevich

unread,
Jun 18, 2014, 1:29:45 PM6/18/14
to django...@googlegroups.com
1) Try to debug your code. Django is open source and sources are available:)
2) Do you have LocaleMiddleware installed?
3) Do not use bare language, use language-region (http://www.i18nguy.com/unicode/language-identifiers.html) : en-gb, en-us, en-bb and so on
4) It is generally bad idea to depend on LANGUAGE_CODE. You should use get_language() to support I18N

Ilya Kazakevich,
JetBrains PyCharm (Best Python/Django IDE)
http://www.jetbrains.com/pycharm/
"Develop with pleasure!"


>-----Original Message-----
>From: django...@googlegroups.com
>[mailto:django...@googlegroups.com] On Behalf Of Stodge
>Sent: Wednesday, June 18, 2014 9:21 PM
>To: django...@googlegroups.com
>Subject: Re: Language code issue - Django thinks default is en-us?
>
>Even when I override the language code in my custom command get_language()
>still returns "en-us". Weird. Guess I'll have to use settings.LANGUAGE_CODE
>instead of get_language().
>
>On Wednesday, 18 June 2014 13:02:37 UTC-4, Stodge wrote:
>
> Oh.
>
> https://docs.djangoproject.com/en/dev/howto/custom-management-com
>mands/#management-commands-and-locales
><https://docs.djangoproject.com/en/dev/howto/custom-management-comman
>ds/#management-commands-and-locales>
>
> I forgot to mention that the signal is executed as a result of a loaddata
>management command. So management commands don't respect
>settings.LANGUAGE_CODE? That makes no sense.
>
> On Wednesday, 18 June 2014 12:48:47 UTC-4, Stodge wrote:
>
> My settings for languages are:
>
>
> LANGUAGE_CODE = 'en'
>
>
> USE_I18N = True
> LANGUAGE_COOKIE_NAME='django_language'
> ugettext = lambda s: s
> LANGUAGES = (
> ('en', ugettext('English')),
> ('de', ugettext('German')),
> ('fr', ugettext('French'))
> )
>
>
> I have a post-save signal that creates instances of a model and gets the
>current Django language. The language returned is for some reason "en-us", but
>I have no idea where this is coming from. My default as shown above is "en".
>get_language() is called from within a signal so the language isn't being taken
>from a cookie or request header. Any ideas why Django thinks the current
>(default) language is "en-us"? Thanks
>
>
>--
>You received this message because you are subscribed to the Google Groups
>"Django users" group.
>To unsubscribe from this group and stop receiving emails from it, send an email to
>django-users...@googlegroups.com.
>To post to this group, send email to django...@googlegroups.com.
>Visit this group at http://groups.google.com/group/django-users.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/django-users/d5dcf509-a634-4f46-940c-8e0
>ee6c0ee69%40googlegroups.com
><https://groups.google.com/d/msgid/django-users/d5dcf509-a634-4f46-940c-8e
>0ee6c0ee69%40googlegroups.com?utm_medium=email&utm_source=footer> .
>For more options, visit https://groups.google.com/d/optout.


Stodge

unread,
Jun 18, 2014, 1:41:35 PM6/18/14
to django...@googlegroups.com, ilya.ka...@jetbrains.com
Thanks. I am using LocaleMiddleware. I changed my languages to:

LANGUAGE_CODE = 'en-gb'
USE_I18N = True
LANGUAGES = (
    ('en-gb', ugettext('English')),
    ('de', ugettext('German')),
    ('fr', ugettext('French'))
)

But I'm still getting en-us.

Ilya Kazakevich

unread,
Jun 18, 2014, 1:48:41 PM6/18/14
to django...@googlegroups.com
LocaleMiddleware sets your language to one, provided by your webbrowser.
Tty to disable LocaleMiddleware or configure your browser to use different language (http://stackoverflow.com/questions/7769061/how-to-add-custom-accept-languages-to-chrome-for-pseudolocalization-testing)


Ilya Kazakevich,
JetBrains PyCharm (Best Python/Django IDE)
http://www.jetbrains.com/pycharm/
"Develop with pleasure!"


>-----Original Message-----
>From: django...@googlegroups.com
>[mailto:django...@googlegroups.com] On Behalf Of Stodge
>Sent: Wednesday, June 18, 2014 9:42 PM
>To: django...@googlegroups.com
>Cc: ilya.ka...@jetbrains.com
>Subject: Re: Language code issue - Django thinks default is en-us?
>
>Thanks. I am using LocaleMiddleware. I changed my languages to:
>
>LANGUAGE_CODE = 'en-gb'
>USE_I18N = True
>LANGUAGES = (
>
> ('en-gb', ugettext('English')),
> ('de', ugettext('German')),
> ('fr', ugettext('French'))
>)
>
>But I'm still getting en-us.
>
>On Wednesday, 18 June 2014 13:29:45 UTC-4, Ilya Kazakevich wrote:
>
> 1) Try to debug your code. Django is open source and sources are available:)
> 2) Do you have LocaleMiddleware installed?
> 3) Do not use bare language, use language-region
>(http://www.i18nguy.com/unicode/language-identifiers.html
><http://www.i18nguy.com/unicode/language-identifiers.html> ) : en-gb, en-us,
>en-bb and so on
> 4) It is generally bad idea to depend on LANGUAGE_CODE. You should use
>get_language() to support I18N
>
> Ilya Kazakevich,
> JetBrains PyCharm (Best Python/Django IDE)
> http://www.jetbrains.com/pycharm/
><http://www.jetbrains.com/pycharm/>
> "Develop with pleasure!"
>
>
> >-----Original Message-----
> >From: django...@googlegroups.com <javascript:>
> >[mailto:django...@googlegroups.com <javascript:> ] On Behalf Of Stodge
> >Sent: Wednesday, June 18, 2014 9:21 PM
> >To: django...@googlegroups.com <javascript:>
> >Subject: Re: Language code issue - Django thinks default is en-us?
> >
> >Even when I override the language code in my custom command
>get_language()
> >still returns "en-us". Weird. Guess I'll have to use
>settings.LANGUAGE_CODE
> >instead of get_language().
> >
> >On Wednesday, 18 June 2014 13:02:37 UTC-4, Stodge wrote:
> >
> > Oh.
> >
> >
>https://docs.djangoproject.com/en/dev/howto/custom-management-com
><https://docs.djangoproject.com/en/dev/howto/custom-management-com>
> >mands/#management-commands-and-locales
> ><https://docs.djangoproject.com/en/dev/howto/custom-management-co
>mman
> >django-users...@googlegroups.com <javascript:> .
> >To post to this group, send email to django...@googlegroups.com
><javascript:> .
><http://groups.google.com/group/django-users> .
> >To view this discussion on the web visit
> >https://groups.google.com/d/msgid/django-users/d5dcf509-a634-4f46-940
>c-8e0
><https://groups.google.com/d/msgid/django-users/d5dcf509-a634-4f46-940c-8e
>0>
> >ee6c0ee69%40googlegroups.com
> ><https://groups.google.com/d/msgid/django-users/d5dcf509-a634-4f46-94
>0c-8e
><https://groups.google.com/d/msgid/django-users/d5dcf509-a634-4f46-940c-8e
>>
> >0ee6c0ee69%40googlegroups.com?utm_medium=email&utm_source=foo
>ter <http://40googlegroups.com?utm_medium=email&utm_source=footer> > .
> >For more options, visit https://groups.google.com/d/optout
><https://groups.google.com/d/optout> .
>
>
>
>
>--
>You received this message because you are subscribed to the Google Groups
>"Django users" group.
>To unsubscribe from this group and stop receiving emails from it, send an email to
>django-users...@googlegroups.com.
>To post to this group, send email to django...@googlegroups.com.
>Visit this group at http://groups.google.com/group/django-users.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/django-users/aec80a5b-3c00-449c-b4f2-351
>d6a0b8816%40googlegroups.com
><https://groups.google.com/d/msgid/django-users/aec80a5b-3c00-449c-b4f2-35
>1d6a0b8816%40googlegroups.com?utm_medium=email&utm_source=footer> .

Stodge

unread,
Jun 18, 2014, 1:51:22 PM6/18/14
to django...@googlegroups.com, ilya.ka...@jetbrains.com
This has nothing to do with the browser - it's a post save signal that executes inside a custom management command.

Thanks again

Ilya Kazakevich

unread,
Jun 18, 2014, 1:57:04 PM6/18/14
to django...@googlegroups.com
Oh, sorry, I forgot it is about manage command.

Try to disable USE_I18N (USE_I18N = False) and check if it works.

See "django\utils\translation\__ini__.py" in your site-libs for details (class Trans, its getattr and get_language() function)



Ilya Kazakevich,
JetBrains PyCharm (Best Python/Django IDE)
http://www.jetbrains.com/pycharm/
"Develop with pleasure!"


>-----Original Message-----
>From: django...@googlegroups.com
>[mailto:django...@googlegroups.com] On Behalf Of Stodge
>Sent: Wednesday, June 18, 2014 9:51 PM
>To: django...@googlegroups.com
>Cc: ilya.ka...@jetbrains.com
>Subject: Re: Language code issue - Django thinks default is en-us?
>
>This has nothing to do with the browser - it's a post save signal that executes
>inside a custom management command.
>
>Thanks again
>
>On Wednesday, 18 June 2014 13:48:41 UTC-4, Ilya Kazakevich wrote:
>
> LocaleMiddleware sets your language to one, provided by your webbrowser.
> Tty to disable LocaleMiddleware or configure your browser to use different
>language
>(http://stackoverflow.com/questions/7769061/how-to-add-custom-accept-langu
>ages-to-chrome-for-pseudolocalization-testing
><http://stackoverflow.com/questions/7769061/how-to-add-custom-accept-langu
>ages-to-chrome-for-pseudolocalization-testing> )
>
>
> Ilya Kazakevich,
> JetBrains PyCharm (Best Python/Django IDE)
> http://www.jetbrains.com/pycharm/
><http://www.jetbrains.com/pycharm/>
> "Develop with pleasure!"
>
>
> >-----Original Message-----
> >From: django...@googlegroups.com <javascript:>
> >[mailto:django...@googlegroups.com <javascript:> ] On Behalf Of Stodge
> >Sent: Wednesday, June 18, 2014 9:42 PM
> >To: django...@googlegroups.com <javascript:>
> >Cc: ilya.ka...@jetbrains.com <javascript:>
> >Subject: Re: Language code issue - Django thinks default is en-us?
> >
> ><https://docs.djangoproject.com/en/dev/howto/custom-management-co
>m <https://docs.djangoproject.com/en/dev/howto/custom-management-com> >
> > >mands/#management-commands-and-locales
> > ><https://docs.djangoproject.com/en/dev/howto/custom-mana
>gement-co
><https://docs.djangoproject.com/en/dev/howto/custom-management-co>
> >mman
><https://groups.google.com/d/msgid/django-users/d5dcf509-a634-4f46-940>
> >c-8e0
><http://40googlegroups.com>
> > ><https://groups.google.com/d/msgid/django-users/d5dcf509-a
>634-4f46-94
><https://groups.google.com/d/msgid/django-users/d5dcf509-a634-4f46-94>
> >0c-8e
><http://40googlegroups.com?utm_medium=email&utm_source=foo>
> >ter
><http://40googlegroups.com?utm_medium=email&utm_source=footer
><http://40googlegroups.com?utm_medium=email&utm_source=footer> > > .
> > >For more options, visit https://groups.google.com/d/optout
><https://groups.google.com/d/optout>
> ><https://groups.google.com/d/optout
><https://groups.google.com/d/optout> > .
> >
> >
> >
> >
> >--
> >You received this message because you are subscribed to the Google
>Groups
> >"Django users" group.
> >To unsubscribe from this group and stop receiving emails from it, send an
>email to
> >django-users...@googlegroups.com <javascript:> .
> >To post to this group, send email to django...@googlegroups.com
><javascript:> .
> >Visit this group at http://groups.google.com/group/django-users
><http://groups.google.com/group/django-users> .
> >To view this discussion on the web visit
> >https://groups.google.com/d/msgid/django-users/aec80a5b-3c00-449c-b4
>f2-351
><https://groups.google.com/d/msgid/django-users/aec80a5b-3c00-449c-b4f2-35
>1>
> >d6a0b8816%40googlegroups.com
> ><https://groups.google.com/d/msgid/django-users/aec80a5b-3c00-449c-b
>4f2-35
><https://groups.google.com/d/msgid/django-users/aec80a5b-3c00-449c-b4f2-35
>>
> >1d6a0b8816%40googlegroups.com?utm_medium=email&utm_source=foo
>ter <http://40googlegroups.com?utm_medium=email&utm_source=footer> > .
> >For more options, visit https://groups.google.com/d/optout
><https://groups.google.com/d/optout> .
>
>
>
>
>--
>You received this message because you are subscribed to the Google Groups
>"Django users" group.
>To unsubscribe from this group and stop receiving emails from it, send an email to
>django-users...@googlegroups.com.
>To post to this group, send email to django...@googlegroups.com.
>Visit this group at http://groups.google.com/group/django-users.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/django-users/a5277e2c-110d-47c3-b53c-cce
>bb2d1ca17%40googlegroups.com
><https://groups.google.com/d/msgid/django-users/a5277e2c-110d-47c3-b53c-cc
>ebb2d1ca17%40googlegroups.com?utm_medium=email&utm_source=footer> .

James Bennett

unread,
Jun 18, 2014, 1:58:05 PM6/18/14
to django...@googlegroups.com
On Wed, Jun 18, 2014 at 10:21 AM, Stodge <sto...@gmail.com> wrote:
Even when I override the language code in my custom command get_language() still returns "en-us". Weird. Guess I'll have to use settings.LANGUAGE_CODE instead of get_language().

Define what you mean by "override the language code". Are you doing what the documentation you linked to says to do in order to activate the locale you want?

Stodge

unread,
Jun 18, 2014, 2:10:08 PM6/18/14
to django...@googlegroups.com
By override I mean I add this at the start of my handle() function in my custom management command:

        from django.utils import translation
        translation.activate(settings.LANGUAGE_CODE)

Where settings.LANGUAGE_CODE = "en-gb".

Calling get_language in my custom management command still returns "en-us".

Tom Evans

unread,
Jun 19, 2014, 8:38:31 AM6/19/14
to django...@googlegroups.com
On Wed, Jun 18, 2014 at 7:10 PM, Stodge <sto...@gmail.com> wrote:
> By override I mean I add this at the start of my handle() function in my
> custom management command:
>
> from django.utils import translation
> translation.activate(settings.LANGUAGE_CODE)
>
> Where settings.LANGUAGE_CODE = "en-gb".
>
> Calling get_language in my custom management command still returns "en-us".
>

In a management command, django will always set the language to "en-us":

https://docs.djangoproject.com/en/1.6/howto/custom-management-commands/#management-commands-and-locales

Cheers

Tom

.

Stodge

unread,
Jun 19, 2014, 8:41:55 AM6/19/14
to django...@googlegroups.com, teva...@googlemail.com
Yes I'm aware of this thanks. However, the documentation also states that I can activate a different language in my custom command:

If, for some reason, your custom management command needs to use a fixed locale different from ‘en-us’, you should manually activate and deactivate it in your handle() or handle_noargs() method using the functions provided by the I18N support code:

Using the code:

        # Activate a fixed locale, e.g. Russian
        translation.activate('ru')

However, this doesn't appear to change anything.

Stodge

unread,
Jun 19, 2014, 8:44:43 AM6/19/14
to django...@googlegroups.com
Sorry, this is using Django 1.6.2.


On Wednesday, 18 June 2014 12:48:47 UTC-4, Stodge wrote:

Tom Evans

unread,
Jun 19, 2014, 9:39:34 AM6/19/14
to django...@googlegroups.com
On Thu, Jun 19, 2014 at 1:41 PM, Stodge <sto...@gmail.com> wrote:
> Yes I'm aware of this thanks. However, the documentation also states that I
> can activate a different language in my custom command:
>
> If, for some reason, your custom management command needs to use a fixed
> locale different from ‘en-us’, you should manually activate and deactivate
> it in your handle() or handle_noargs() method using the functions provided
> by the I18N support code:
>
>
> Using the code:
>
> # Activate a fixed locale, e.g. Russian
> translation.activate('ru')
>
>
> However, this doesn't appear to change anything.
>

From an earlier email, 'ru' is not an enabled language in your settings.

Cheers

Tom

Stodge

unread,
Jun 19, 2014, 9:49:09 AM6/19/14
to django...@googlegroups.com, teva...@googlemail.com

Stodge

unread,
Jun 19, 2014, 10:18:32 AM6/19/14
to django...@googlegroups.com
My problem seems to be this:

 * run custom management command
 * activate language from settings, which is "en-gb"
 * dynamically import module for a Django app and invoke custom bootstrap function
 * calling get_language() in bootstrap function returns "en-gb"
 * bootstrap function calls loaddata management command (using management.call_command)
 * data created in DB, which triggers a post_save signal
 * get_language() in post_save signal always returns "en-us"




On Wednesday, 18 June 2014 12:48:47 UTC-4, Stodge wrote:

Vernon D. Cole

unread,
Jun 20, 2014, 8:47:13 AM6/20/14
to django...@googlegroups.com
(*cough*)
Excuse me, everyone, but many of the locale names mentioned in this discussion have been invalid.

https://docs.djangoproject.com/en/1.6/topics/i18n/#term-locale-name says:
locale name
A locale name, either a language specification of the form ll or a combined language and country specification of the form ll_CC. Examples: it, de_AT, es, pt_BR. The language part is always in lower case and the country part in upper case. The separator is an underscore.

so 'en-gb' should not be recognized.
'en_GB' ought to be.

Stodge

unread,
Jun 20, 2014, 9:07:32 AM6/20/14
to django...@googlegroups.com
This always confuses me - the language code is always lower case according to the docs. Which is specified in the settings - language codes or locales? I've always assumed the former.

locale name
A locale name, either a language specification of the form ll or a combined language and country specification of the form ll_CC. Examples: itde_ATespt_BR. The language part is always in lower case and the country part in upper case. The separator is an underscore.
language code
Represents the name of a language. Browsers send the names of the languages they accept in the Accept-Language HTTP header using this format. Examples: itde-atespt-br. Both the language and the country parts are in lower case. The separator is a dash.

Tom Evans

unread,
Jun 20, 2014, 9:22:53 AM6/20/14
to django...@googlegroups.com
On Fri, Jun 20, 2014 at 1:47 PM, Vernon D. Cole <verno...@gmail.com> wrote:
> (*cough*)
> Excuse me, everyone, but many of the locale names mentioned in this
> discussion have been invalid.
>
> https://docs.djangoproject.com/en/1.6/topics/i18n/#term-locale-name says:
>>
>> locale nameA locale name, either a language specification of the form ll
>> or a combined language and country specification of the form ll_CC.
>> Examples: it, de_AT, es, pt_BR. The language part is always in lower case
>> and the country part in upper case. The separator is an underscore.
>
>
> so 'en-gb' should not be recognized.
> 'en_GB' ought to be.
>

What is and what is not a locale name has little bearing on what is
accepted and returned by django.utils.translation.{set,get}_language,
which is self-evidently a language name.

Language names map to locales.

Cheers

Tom

rezn...@gmail.com

unread,
Jul 21, 2023, 4:28:16 AM7/21/23
to Django users
Create a new middleware

from django.utils.translation import activate
from django.conf import settings

<pre>
class CustomLanguageMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # Read the language preference from the local storage cookie
        language_preference = request.COOKIES.get('language_preference', None) # Instead of `None` set your default language code in case of there is no language_preference comes with cookies

        # If a language preference is found, set the 'Accept-Language' header
        if language_preference and language_preference in dict(settings.LANGUAGES):
            request.META['HTTP_ACCEPT_LANGUAGE'] = language_preference

        # Call the next middleware or view
        response = self.get_response(request)

        # If you want, you can also update the 'language' in the session
        if language_preference and language_preference in dict(settings.LANGUAGES):
            activate(language_preference)

        return response
</pre>

add it right before `django.middleware.locale.LocaleMiddleware` 

DJANGO_MIDDLEWARES = [
    # add your custom middleware here
    'django.middleware.locale.LocaleMiddleware',
]

this custom middleware will override the default behavior of the LocalMiddleware.

Finally, you can store the language preference in the local storage cookie:

<pre>
const languagePreference = 'ru-RU'; // Replace this with the user's preferred language
localStorage.setItem('language_preference', languagePreference);
</pre>
Reply all
Reply to author
Forward
0 new messages