Mercurial on Mac OS X: ValueError: unknown locale: UTF-8

Mercurial from macports on Mac OX X Leopard gives this error message on any hg command:

Traceback (most recent call last):
File “/opt/local/bin/hg”, line 25, in
File “/opt/local/lib/python2.5/site-packages/mercurial/”, line 75, in getattribute
File “/opt/local/lib/python2.5/site-packages/mercurial/”, line 47, in _load
mod = _origimport(head, globals, locals)
File “/opt/local/lib/python2.5/site-packages/mercurial/”, line 93, in
_encoding = locale.getlocale()[1]
File “/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/”, line 462, in getlocale
return _parse_localename(localename)
File “/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/”, line 375, in _parse_localename
raise ValueError, ‘unknown locale: %s’ % localename
ValueError: unknown locale: UTF-8

The mercurial-user list has a posting from October 2007 that blames the Mac OS X terminal application.

This really seems to be a problem with the terminal application, because if you start X11 and xterm, hg will start without the error message. In order to work with mercurial on Terminal you need to set LC_ALL and LANG in your .bashrc or .profile, like

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8