开发者

Is it possible to reset locale to the portable locale?

According to the Python documentation:

"a program which has not called setlocale(LC_ALL, '') runs using the portable 'C' locale.

After having set the locale with setlocale(LC_ALL, '') is it possible to reset the locale back to the "portable" state? I work on a library which includes some misbehaved components which attempt to globally reset the locale to the region specific - I need to find a way to revert the locale back to the portable state.

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 t开发者_运维问答his true!

Needs to work on Python 2.4.4 on Windows XP 32bit


You could try: 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)


locale.setlocale(locale.getdefaultlocale())

Will set it back to the standard locale

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜