[Django]-Django: Display current locale in a template

53πŸ‘

βœ…

I solved this by including code below in the template

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

and the variable LANGUAGE_CODE has the value you want (see also django docs for an example usage).

14πŸ‘

You might want to write your own context processor, which would call to_locale and automatically populate the context with the result β€” it would just be something like this.

from django.utils.translation import to_locale, get_language
def locale(request):
    return {'LOCALE': to_locale(get_language())}
πŸ‘€Ismail Badawi

3πŸ‘

I thought of implementing my own custom template tag that would simply output to_locale(get_language()) but the answer above is easier to implement so I like it better.

πŸ‘€urig

Leave a comment