Mathias Panzenböck
2009-01-24 23:48:20 UTC
When I generate certain files I have to write a date formated as english text,
but the error messages (if there are any) I'd like to keep in the system
language. I propose to add this (or a similar) contextmanager:
@contextmanager
def localectx(category, newlocale=None):
oldlocale = locale.getlocale(category)
locale.setlocale(category, newlocale)
try:
yield
finally:
locale.setlocale(category, oldlocale)
Example usage:
with localectx(locale.LC_TIME,('en','UTF8')):
sdate = time.strftime('%a %b %d %Y')
spec_file.write('''
%%changelog
* %s Maintainer Name <maintainer-***@public.gmane.org>
''' % sdate)
-panzi
but the error messages (if there are any) I'd like to keep in the system
language. I propose to add this (or a similar) contextmanager:
@contextmanager
def localectx(category, newlocale=None):
oldlocale = locale.getlocale(category)
locale.setlocale(category, newlocale)
try:
yield
finally:
locale.setlocale(category, oldlocale)
Example usage:
with localectx(locale.LC_TIME,('en','UTF8')):
sdate = time.strftime('%a %b %d %Y')
spec_file.write('''
%%changelog
* %s Maintainer Name <maintainer-***@public.gmane.org>
''' % sdate)
-panzi