@contextmanager<EOL>def set_locale(new_locale, lc_var=locale.LC_ALL):
current_locale = locale.getlocale()<EOL>try:<EOL><INDENT>locale.setlocale(lc_var, new_locale)<EOL>normalized_locale = locale.getlocale()<EOL>if all(x is not None for x in normalized_locale):<EOL><INDENT>yield '<STR_LIT:.>'.join(normalized_locale)<EOL><DEDENT>else:<EOL><INDENT>yield new_locale<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>locale.setlocale(lc_var, current_locale)<EOL><DEDENT>
Context manager for temporarily setting a locale.

Parameters
----------
new_locale : str or tuple
    A string of the form <language_country>.<encoding>. For example to set
    the current locale to US English with a UTF8 encoding, you would pass
    "en_US.UTF-8".
lc_var : int, default `locale.LC_ALL`
    The category of the locale being set.

Notes
-----
This is useful when you want to run a particular block of code under a
particular locale, without globally setting the locale. This probably isn't
thread-safe.