You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The example seems to fail on Ubuntu, but runs on Windows - there is a comment right above the line that says name might vary with platform. It does seem that locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') works on both platforms though, so maybe it can be changed to that
Depends on what locales are installed; on a minimal Ubuntu container not even 'de_DE.UTF-8' will work.
IMO it would be good to mention 'de_DE.UTF-8' in the docs, but also note that locale availability (and not just naming) might vary with platform.
>>> import locale
>>> loc = locale.getlocale() # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo') # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C') # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc) # restore saved locale
However locale.getlocale() does not return the locale for all categories (locale.LC_ALL is even not allowed) but the locale for the LC_CTYPE category.
Therefore restoring it using locale.setlocale(locale.LC_ALL, loc) does not actually restore the initial settings, and may even fail on some platforms (on mine it does).
The correct example should have the first line of code replaced with
>>> loc = locale.setlocale(locale.LC_ALL) # get current locale
Activity
pintman commentedon Jun 19, 2020
The example given results in an error.
https://docs.python.org/3.8/library/locale.html?highlight=locale
I could be improved with one of the following versions:
Barathwaja commentedon Aug 16, 2020
Hi Marco,
Can I work on this?
pintman commentedon Aug 17, 2020
I have only reported the bug and do not know exactly how to proceed. I don't think there's anything wrong with taking over the bug.
slateny commentedon Apr 22, 2022
The example seems to fail on Ubuntu, but runs on Windows - there is a comment right above the line that says
name might vary with platform. It does seem thatlocale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')works on both platforms though, so maybe it can be changed to that3 remaining items
encukou commentedon Feb 24, 2025
Depends on what locales are installed; on a minimal Ubuntu container not even
'de_DE.UTF-8'will work.IMO it would be good to mention
'de_DE.UTF-8'in the docs, but also note that locale availability (and not just naming) might vary with platform.picnixz commentedon Mar 23, 2025
Adding an other example from #91343 (comment):
gh-85204: Improve `locale.setlocale` example (#132683)
pythongh-85204: Improve `locale.setlocale` example (pythonGH-132683)
pythongh-85204: Improve `locale.setlocale` example (pythonGH-132683)
[3.14] gh-85204: Improve `locale.setlocale` example (GH-132683) (#142798
[3.13] gh-85204: Improve `locale.setlocale` example (GH-132683) (#142799