Search code examples
pythondjangodjango-templatestranslationdjango-i18n

How to translate dates in django template?


I've read documentation, I put i18n in top and tried to translate date variables by:

{%  blocktrans %}
                   {{ month_verbose }}
{% endblocktrans %}

But it doesn't work, also I want to translate month_verbose to current language. I don't know how to combine current language with translation with dates. So what is the best way to translate dates in django templates?

UPD View for this template Var month verbose is on 39th row Template here month verbose on 33rd row


Solution

  • Add django.middleware.locale.LocaleMiddleware to settings.MIDDLEWARE_CLASSES.

    You can remove the blocktrans tags. Django date filter values are localized automatically when USE_L10N=True, so there's no need to use localize template tag or filter.

    # prints out "uk"
    {{ request.LANGUAGE_CODE }} 
    
    # prints out "Січ"
    {{ month_verbose|date:"M" }}