From 47a658c475bf1380b06574704d500497abcd03ef Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Thu, 5 Feb 2009 14:18:12 +0100 Subject: [PATCH] [FIX] locale problems (again) bzr revid: christophe@tinyerp.com-20090205131812-btihwg9g1qweyln3 --- bin/netsvc.py | 16 +++++++++------- bin/tools/misc.py | 4 ++-- bin/tools/translate.py | 3 +-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/bin/netsvc.py b/bin/netsvc.py index 656fde1ef22..d121e4b5b39 100644 --- a/bin/netsvc.py +++ b/bin/netsvc.py @@ -88,29 +88,31 @@ LOG_CRITICAL = 'critical' logging.DEBUG_RPC = logging.DEBUG - 1 class UnicodeFormatter(logging.Formatter): - def __init__(self, fmt=None, datefmt=None): + def __init__(self, enc, fmt=None, datefmt=None): + self._enc = enc logging.Formatter.__init__(self, fmt, datefmt) self._fmt = tools.ustr(self._fmt) if self.datefmt is not None: self.datefmt = tools.ustr(self.datefmt) def formatTime(self, *args, **kwargs): - return tools.ustr(logging.Formatter.formatTime(self, *args, **kwargs)) + return tools.ustr(logging.Formatter.formatTime(self, *args, **kwargs), self._enc) def formatException(self, *args, **kwargs): - return tools.ustr(logging.Formatter.formatException(self, *args, **kwargs)) + return tools.ustr(logging.Formatter.formatException(self, *args, **kwargs), self._enc) def format(self, *args, **kwargs): - return tools.ustr(logging.Formatter.format(self, *args, **kwargs)) + return tools.ustr(logging.Formatter.format(self, *args, **kwargs), self._enc) def init_logger(): import os from tools.translate import resetlocale - resetlocale() + l = resetlocale() or '' + enc = '.' in l and l.split('.')[1] or 'utf-8' logger = logging.getLogger() # create a format for log messages and dates - formatter = UnicodeFormatter('[%(asctime)s] %(levelname)s:%(name)s:%(message)s', '%a %b %d %Y %H:%M:%S') + formatter = UnicodeFormatter(enc, '[%(asctime)s] %(levelname)s:%(name)s:%(message)s', '%a %b %d %Y %H:%M:%S') logging_to_stdout = False if tools.config['syslog']: @@ -121,7 +123,7 @@ def init_logger(): release.version)) else: handler = logging.handlers.SysLogHandler('/dev/log') - formatter = UnicodeFormatter("%s %s" % (release.description, release.version) + ':%(levelname)s:%(name)s:%(message)s') + formatter = UnicodeFormatter(enc, "%s %s" % (release.description, release.version) + ':%(levelname)s:%(name)s:%(message)s') elif tools.config['logfile']: # LogFile Handler diff --git a/bin/tools/misc.py b/bin/tools/misc.py index 04a1bba0ab5..0a47ae044ec 100644 --- a/bin/tools/misc.py +++ b/bin/tools/misc.py @@ -658,7 +658,7 @@ class cache(object): def to_xml(s): return s.replace('&','&').replace('<','<').replace('>','>') -def ustr(value): +def ustr(value, fromenc='utf-8'): """This method is similar to the builtin `str` method, except it will return Unicode string. @@ -677,7 +677,7 @@ def ustr(value): if not isinstance(value, str): value = str(value) - return unicode(value, 'utf-8') + return unicode(value, fromenc) def exception_to_unicode(e): if hasattr(e, 'message'): diff --git a/bin/tools/translate.py b/bin/tools/translate.py index 8b5e68e4e1b..b2ef256d11a 100644 --- a/bin/tools/translate.py +++ b/bin/tools/translate.py @@ -683,8 +683,7 @@ def resetlocale(): # locale.resetlocale is bugged with some locales. for ln in get_locales(): try: - locale.setlocale(locale.LC_ALL, ln) - break + return locale.setlocale(locale.LC_ALL, ln) except locale.Error: continue