13

What do I need to do (modules to load, locale methods to invoke, etc.) so that when I call:

datetime.date(2009,1,16).strftime("%A %Y-%b-%d")

instead of getting:

Out[20]: 'Friday 2009-Jan-16'

i get spanish/french/german/... output

Out[20]: 'Viernes 2009-Ene-16'

without having to change my whole operating system's locale (i.e. just use python calls to dynamically set the locale and keep the changes scoped within my app)

Thanks.

jd.
  • 4,543
  • 7
  • 34
  • 40

4 Answers4

8

locale.setlocale()

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
6

On Ubuntu,

$> sudo locale-gen es_ES.UTF-8
$> sudo dpkg-reconfigure locales
$> python
>>> import locale
>>> locale.setlocale(locale.LC_TIME, 'es_ES.UTF-8')
e18r
  • 7,578
  • 4
  • 45
  • 40
4

Also, have a look at the babel project.

codeape
  • 97,830
  • 24
  • 159
  • 188
2

After setting your locale (with locale.setlocale) You can use the locale modules' nl_langinfo method like so:

time.strftime(locale.nl_langinfo(locale.D_T_FMT), time.localtime())
Kyle Lutz
  • 7,966
  • 2
  • 20
  • 23