Python Django: make strftime return german language, not english

47 Views Asked by At

In my Django project, I'm using strftime, specifically %b to get the abbreviated month name. Those names are returned in english. My settings contain the following:

LANGUAGE_CODE = 'de' # also tried 'de_DE'
USE_I18N = True
USE_TZ = True

Why does strftime still return english language?

2

There are 2 best solutions below

1
Lukas Thaler On

There's a few things you could try:

  1. According to the docs you should be supplying de-de to LANGUAGE_CODE.
  2. You might also want to try setting LOCALE_NAME = 'de_DE'
  3. If all else fails, you could always try to brute force it via import locale and locale.setlocale(locale.LC_TIME, 'de_DE') (or de_DE.utf8')
0
R-obert On

Okay, I found the answer. It is a mix of this post (background understanding) and this post (solution). Apparently strftime does not access whatever you have specified in your Django settings. It will just use the locale of the machine. So instead you can use django.template.defaultfilters. Import it in your views under whatever name you like (e.g. import django.template.defaultfilters as myvarname) and then use it to format your date objects: myvarname(yourdateobject, 'b').