Context
Getting the locale with python on Windows seems to be broken:
(trash0) PS C:\Users\myname\venv\trash0\Lib\site-packages> python.exe
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.platform
'win32'
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'fr-FR')
'fr-FR'
>>> locale.getlocale()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python36-32\lib\locale.py", line 581, in getlocale
return _parse_localename(localename)
File "C:\Program Files\Python36-32\lib\locale.py", line 490, in _parse_localename
raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: fr-FR
>>>
I do not know much about Windows but I have checked fr-FR
belongs to the correct locale names for Windows. Note that using en-US
or en-GB
get the same result.
Yet setting the locale works correctly because:
- using
locale.setlocale()
with any unknown value would raise an exception:
>>> locale.setlocale(locale.LC_ALL, 'anythingundefined')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python36-32\lib\locale.py", line 598, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
>>>
- once set, it's possible to check it is taken into account:
>>> locale.setlocale(locale.LC_ALL, 'fr-FR')
'fr-FR'
>>> locale.str(12.3)
'12,3'
>>> locale.setlocale(locale.LC_ALL, 'en-GB')
'en-GB'
>>> locale.str(12.3)
'12.3'
>>>
Question
I need to temporarily set the locale to en-US
(in order to perform some things that require this locale) and then switch back to the default locale. How is it possible to do it if locale.getlocale()
is broken? I've read the python doc about locale but can't figure out any workaround to achieve this (nor whether it is possible at all).