diff options
| author | Jay (Open ERP) <jvo@tinyerp.com> | 2010-05-20 12:00:44 +0530 |
|---|---|---|
| committer | Jay (Open ERP) <jvo@tinyerp.com> | 2010-05-20 12:00:44 +0530 |
| commit | e3ccef27d33ff83ddbb7bfd3eacc80f8a98bd21d (patch) | |
| tree | f14fb77ab9702c91bcb9309724e5aa63a96ee570 /bin/tools/misc.py | |
| parent | 2669744f4f09ca6ac9219dc91909e895559c74c0 (diff) | |
| parent | fe7673b7ba1f2706b8ed2abe6892ad33211063cd (diff) | |
[BACKPORT] Stable fixes and refactorings to Trunk
bzr revid: jvo@tinyerp.com-20100520063044-sviy1e9pybirhpsw
Diffstat (limited to 'bin/tools/misc.py')
| -rw-r--r-- | bin/tools/misc.py | 46 |
1 files changed, 29 insertions, 17 deletions
diff --git a/bin/tools/misc.py b/bin/tools/misc.py index 07e0fc38f8e..e60213ca740 100644 --- a/bin/tools/misc.py +++ b/bin/tools/misc.py @@ -820,6 +820,22 @@ class cache(object): def to_xml(s): return s.replace('&','&').replace('<','<').replace('>','>') +def get_encodings(): + yield 'utf8' + from locale import getpreferredencoding + prefenc = getpreferredencoding() + if prefenc: + yield prefenc + + prefenc = { + 'latin1': 'latin9', + 'iso-8859-1': 'iso8859-15', + 'cp1252': '1252', + }.get(prefenc.lower()) + if prefenc: + yield prefenc + + def ustr(value): """This method is similar to the builtin `str` method, except it will return Unicode string. @@ -829,29 +845,25 @@ def ustr(value): @rtype: unicode @return: unicode string """ + orig = value + if isinstance(value, Exception): + return exception_to_unicode(value) if isinstance(value, unicode): return value - if hasattr(value, '__unicode__'): + try: return unicode(value) - - if not isinstance(value, str): - value = str(value) - - try: # first try utf-8 - return unicode(value, 'utf-8') except: pass - try: # then extened iso-8858 - return unicode(value, 'iso-8859-15') - except: - pass + for ln in get_encodings(): + try: + return unicode(value, ln) + except: + pass + raise UnicodeError('unable de to convert %r' % (orig,)) - # else use default system locale - from locale import getlocale - return unicode(value, getlocale()[1]) def exception_to_unicode(e): if (sys.version_info[:2] < (2,6)) and hasattr(e, 'message'): @@ -914,7 +926,7 @@ def get_languages(): 'fr_BE': u'French (BE) / Français (BE)', 'fr_CH': u'French (CH) / Français (CH)', 'fr_FR': u'French / Français', - 'gl_ES': u'Galician / Spain', + 'gl_ES': u'Galician / Galego', 'gu_IN': u'Gujarati / India', 'hi_IN': u'Hindi / India', 'hr_HR': u'Croatian / hrvatski jezik', @@ -939,8 +951,8 @@ def get_languages(): 'ro_RO': u'Romanian / limba română', 'ru_RU': u'Russian / русский язык', 'si_LK': u'Sinhalese / Sri Lanka', - 'sk_SK': u'Slovak / Slovakia', - 'sl_SL': u'Slovenian / slovenščina', + 'sl_SI': u'Slovenian / slovenščina', + 'sk_SK': u'Slovak / Slovenský jazyk', 'sq_AL': u'Albanian / Shqipëri', 'sr_RS': u'Serbian / Serbia', 'sv_SE': u'Swedish / svenska', |
