0

I wonder if there's a way in the django template system to format the date depending on the language.

Like if my site is in english this code {{ submitted_date | date }} will be displayed like this : June 29, 2016

But if the site is displayed in french the same code should display this format : 27 juin 2016.

Is there a way to tell Django to do this or I just have to hardcode it with if statements? (But if I got many languages?!)

Sayse
  • 42,633
  • 14
  • 77
  • 146
Emmurd
  • 67
  • 1
  • 8

2 Answers2

2

The very first sentence in the locale docs for date formatting says

Django’s formatting system is capable of displaying dates, times and numbers in templates using the format specified for the current locale. It also handles localized input in forms.

So you don't need to do anything, dates are localized to the current locale by default.

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Indeed it change the language of the date (like the month), but it don't change the format. In french, the month is suppose to be after the date and no comma before the year. Is there a way I can force that? – Emmurd Jun 30 '16 at 14:46
  • 1
    I think this is the option for me. https://docs.djangoproject.com/en/1.9/topics/i18n/formatting/#creating-custom-format-files – Emmurd Jun 30 '16 at 14:55
  • If I look at this file https://github.com/django/django/blob/master/django/conf/locale/fr/formats.py the format is good. There's something odd with the locale switch maybe? – Emmurd Jun 30 '16 at 15:17
  • @Emmurd - Its possible, it could be using the local machines(browsers?) locale instead of the set locale, have you made sure that is changed also? – Sayse Jun 30 '16 at 15:24
0

For a reason I don't know, by default django don't use the format settings set by the defaults files.

Per example : https://github.com/django/django/blob/master/django/conf/locale/fr/formats.py

By adding the name of the desired format, the template display the good format for the locale. {{ submitted_date | date:"DATE_FORMAT" }}

Thanks for getting me on the track!

Emmurd
  • 67
  • 1
  • 8