[Tutor] locale.set/getlocale asymmetry

eryksun eryksun at gmail.com
Wed Feb 20 17:16:17 CET 2013

On Wed, Feb 20, 2013 at 8:14 AM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
> Thank you. So can I simply use the function below to replace locale.getlocale()?
> Will this never cause problems? The thing I remember about setlocale is that
> "thou shalt never call setlocale anywhere else but in the beginning of a script".
> Or am I wrong here? I am always a little reluctant to mess with the host locale.
> def getlocale(category=locale.LC_ALL):
>     return locale.setlocale(category)

That's fine. If you only pass in the category it's just a query.

locale.getlocale defaults to a single category (LC_CTYPE) because of
the parsing it does to create the tuple. The issue is that an LC_ALL
query returns all of the categories, delimited by semicolons, if
they're not all the same. In this case locale.getlocale(LC_ALL) raises
a TypeError. But there's no problem using such a locale string with
setlocale LC_ALL.

Also, on Windows locale.getdefaultlocale uses win32 GetLocaleInfo, set
to return ISO 639/3166 language/country codes. These codes don't work
with the MS CRT setlocale, so locale.resetlocale fails.
setlocale(LC_ALL, '') is supported to reset to the default. More here:


language strings

country/region strings

More information about the Tutor mailing list