python-babel / babel

The official repository for Babel, the Python Internationalization Library
http://babel.pocoo.org/
BSD 3-Clause "New" or "Revised" License
1.31k stars 438 forks source link

format_timedelta throws / default_locale('LC_CTIME') is None with POSIX locale #137

Open tko opened 9 years ago

tko commented 9 years ago
import datetime
from babel.dates import format_timedelta
format_timedelta(datetime.timedelta(days=42))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "virtualenv/local/lib/python2.7/site-packages/babel/dates.py", line 779, in format_timedelta
    plural_form = locale.plural_form(value)
AttributeError: 'NoneType' object has no attribute 'plural_form'

This happens when no locale related environment variables are set, e.g.

$ env | egrep 'LANG|LC_'
(nothing)
$ locale
LANG=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
benselme commented 9 years ago

Confirmed. We should certainly fallback on something. Possibly the root locale.