1

根据 Python 文档:

“未调用的程序setlocale(LC_ALL, '')使用可移植的‘C’语言环境运行。

设置语言环境后,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 32bit 上使用 Python 2.4.4

4

2 回答 2

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)
于 2010-11-29T20:06:24.657 回答
2
locale.setlocale(locale.getdefaultlocale())

将其设置回标准语言环境

于 2010-11-29T16:01:02.017 回答