Temporarily override locale with a context manager

前端 未结 3 975
有刺的猬
有刺的猬 2021-01-20 18:36

Is there a way to temporarily activate a locale within the scope of a block of code? Basically, I want to do something like this:

locale.setlocale(locale.LC_         


        
3条回答
  •  别那么骄傲
    2021-01-20 19:04

    Unsure whether you really want to do that. The locale may be global(*) to the program, so it could give a weird behaviour in a multithreaded context. Worse, the standard library documentation says:

    The C standard defines the locale as a program-wide property that may be relatively expensive to change. On top of that, some implementation are broken in such a way that frequent locale changes may cause core dumps.

    That being said, it is possible to build a custom context manager:

    class LocaleManager:
        def __init__(self, localename):
            self.name = localename
        def __enter__(self):
            self.orig = locale.setlocale(locale.LC_CTYPE)
            locale.setlocale(locale.LC_ALL, self.name)
        def __exit__(self, exc_type, exc_value, traceback):
            locale.setlocale(locale.LC_ALL, self.orig)
    

    Example on a French Windows:

    >>> print(locale.getlocale())
    ('fr_FR', 'cp1252')
    >>> with LocaleManager("C"):
        print(locale.getlocale())
    
    
    (None, None)
    >>> print(locale.getlocale())
    ('fr_FR', 'cp1252')
    

提交回复
热议问题