Index ¦ Archives ¦ Atom

Managing locale with Linux and Python

20 September 2016

As far as I can remember I have always installed my operating systems to use the english language, maybe because I did not have the luxury of the choice before and it kind of feels more natural now.

Lately I have had to print names (month, days) of the calendar in french using Python, so I dig a little in "locale", which are the set of parameters that configure the language and region settings used by the system.

On Debian to get the list of "locale" installed

locale -a

To choose which "locale" to make available on the system you can manually edit the file locale.gen by removing the # in front of the line you want activated, followed by the command to generate the modification :

sudo vim /etc/locale.gen
locale-gen

Or simply run this configuration command :

sudo dpkg-reconfigure locales

Now that the correct "locale" is installed on the system, it is possible to display a date with the months and days in the desired language :

import locale
import datetime.date

d = datetime.date.today()
locale.setlocale(locale.LC_ALL, 'fr_CH.utf8')

d.strftime('%d %B %Y')