49

Is there's some global variable for gettin' language code in django template or atleast passing it through view? something like: {{ LANG }} should produce "en" for example.. I really not comfortable when people using request.LANGUAGE_CODE.

Detailed explanation would be appreciated =)

holms
  • 9,112
  • 14
  • 65
  • 95
  • 6
    did you tried {{LANGUAGE_CODE}}, see https://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-i18n for more info – Gagandeep Singh Nov 25 '11 at 05:31

3 Answers3

134

It's an old topic. But some might find it useful.

{% load i18n %}
...
{% get_current_language as LANGUAGE_CODE %}

Django reference and example.

Robert Lujo
  • 15,383
  • 5
  • 56
  • 73
Rafael
  • 2,138
  • 2
  • 16
  • 10
25

If it didn't already exist, you would need to write a template context processor. Here's how you'd do that.

Put this somewhere:

def lang_context_processor(request):
    return {'LANG': request.LANGUAGE_CODE}

And then, add a reference to it the TEMPLATE_CONTEXT_PROCESSORS setting. Something like this:

from django.conf import global_settings

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    'myproject.myapp.templatecontext.lang_context_processor',
)

(I recommend adding to the global setting because it means you don't break things accidentally when a new context processor is added to the defaults.)

However, it does exist, as the inbuilt template context processor django.template.context_processors.i18n. You can access it as LANGUAGE_CODE.

Purely for interest, here's the definition of that function:

def i18n(request):
    from django.utils import translation
    return {
        'LANGUAGES': settings.LANGUAGES,
        'LANGUAGE_CODE': translation.get_language(),
        'LANGUAGE_BIDI': translation.get_language_bidi(),
    }

Make sure that you're using a RequestContext for your template rendering, not a plain Context, or it won't work.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • 3
    (+1) Honestly, use `RequestContext`, always. Just get in the habit now. Using `Context` is a mistake; there will *always* be something later that will make you wish you'd used `RequestContext` universally. – Luke Sneeringer Nov 25 '11 at 12:06
  • 1
    This is a good answer, it is well explained and detailed. But I think it would be easier to read if instead of being written in the order: how you could implement it, what django gives you, example of function. It was written with the actual answer of django i18n first, and then go on with the rest. It feels like the actual answer is burried. – gdvalderrama Dec 02 '16 at 15:47
  • FYI: newer versions of Django have template context processors defined on the `OPTIONS` portion of `TEMPLATES` – Tim Tisdall Mar 29 '22 at 12:51
17

Tested with Django==1.11.2.

Enable I18N and employ i18n template context processor.

# setings.py

USE_I18N = True
# ...
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # ...
                'django.template.context_processors.i18n',
                # ...
            ],
        },
    },
]

And then it's simple in the template.

# template.html

{% load i18n %}
{{ LANGUAGE_CODE }}


But use render(), not render_to_response(), in your view function so the LANGUAGE_CODE variable is accessible in the template:

render_to_response()

This function preceded the introduction of render() and works similarly except that it doesn’t make the request available in the response. It’s not recommended and is likely to be deprecated in the future.

Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64