I need to embed the current locale in a Django template's output (as part of a URL to be precise). I know that I can access the current language as {{ LANGUAGE_CODE }}
if I { load i18n }
but is there a similar way to access the current locale?
I suppose I could use to_locale()
in the view logic and put it in the context for the template, but I'm looking for something more generic that might be part of the Django framework itself. Is there such a syntax?
get_language() which returns the language used in the current thread. See documentation. Caveat: Returns None if translations are temporarily deactivated (by deactivate_all() or when None is passed to override()). Before Django 1.8, get_language() always returned LANGUAGE_CODE when translations were deactivated.
The formatting system is disabled by default. To enable it, it's necessary to set USE_L10N = True in your settings file. Note. To enable number formatting with thousand separators, it is necessary to set USE_THOUSAND_SEPARATOR = True in your settings file.
Default language strings are not stored in po/mo files, they go directly in code and templates - seems that you have this right. You can switch back to it, by setting the session variable django_language back to dutch. This made it work for me, thanks a lot.
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())}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With