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_
You can write an easy and straight-forward contextmanager for this:
from locale import getlocale, setlocale
from contextlib import contextmanager
@contextmanager
def override_locale(category, locale_string):
prev_locale_string = getlocale(category)
setlocale(category, locale_string)
yield
setlocale(category, prev_locale_string)