根据 Python 文档:
"a program which has not called
setlocale(LC_ALL, '')runs using the portable 'C' locale.
在使用 setlocale(LC_ALL, '') 设置语言环境后,是否可以将语言环境重置回“便携”状态?我在一个库中工作,其中包含一些行为不端的组件,这些组件试图将语言环境全局重置为特定区域 - 我需要找到一种方法将语言环境恢复为可移植状态。
import locale
loc = locale.getlocale(locale.LC_ALL) # get current locale
assert loc == (None, None)
# Locale is unset, therefore in the "portable" state.
locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
# Loc is not set to regional default
???? DO SOMETHING HERE
assert loc == locale.getlocale(locale.LC_ALL) # I want to make this true!
需要在 Windows XP 32 位上使用 Python 2.4.4
请您参考如下方法:
您可以尝试:locale.setlocale(locale.LC_ALL, loc)。
>>> locale.getlocale(locale.LC_ALL)
(None, None)
>>> locale.setlocale(locale.LC_ALL, "")
'en_US.utf8'
>>> locale.getlocale(locale.LC_ALL)
('en_US', 'UTF8')
>>> locale.setlocale(locale.LC_ALL, "C")
'C'
>>> locale.getlocale(locale.LC_ALL)
(None, None)
>>> locale.setlocale(locale.LC_ALL, (None,None))
'C'
>>> locale.getlocale(locale.LC_ALL)
(None, None)
