From 9979fd8792ce3604b30c6bbef9df8005f953ea0b Mon Sep 17 00:00:00 2001 From: John Ralls Date: Thu, 4 Apr 2013 21:18:29 +0000 Subject: [PATCH] [r21882]GrampsLocale: Improve windows handling of no locale svn: r21885 --- gramps/gen/utils/grampslocale.py | 37 +++++++++++++------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/gramps/gen/utils/grampslocale.py b/gramps/gen/utils/grampslocale.py index 3fc96edf8..11e4785fe 100644 --- a/gramps/gen/utils/grampslocale.py +++ b/gramps/gen/utils/grampslocale.py @@ -103,7 +103,7 @@ def _check_mswin_locale(locale): msloc = mslocales[locale[:2]] locale = locale[:2] except KeyError: - return None + return (None, None) return (locale, msloc) def _check_mswin_locale_reverse(locale): @@ -111,7 +111,7 @@ def _check_mswin_locale_reverse(locale): if msloc and locale == msloc[0]: return (loc, msloc[1]) - return None + return (None, None) #------------------------------------------------------------------------ # @@ -224,14 +224,20 @@ class GrampsLocale(object): self.collation = self.lang else: (coll, loc) = _check_mswin_locale(coll) - if loc: - locale.setlocale(locale.LC_COLLATE, '.'.join(loc)) - self.collation = coll - else: #can't set the collation locale if MS doesn't support it - self.collation = self.lang - + if not loc: + (coll, loc) = _check_mswin_locale(self.lang) + self.collation = '.'.join(loc) + locale.setlocale(locale.LC_COLLATE, self.collation ) else: - self.collation = self.lang + if HAVE_ICU: + self.collation = self.lang + else: + (coll, loc) = _check_mswin_locale(self.lang) + if loc: + self.collation = '.'.join(loc) + else: + self.collation = 'C' + locale.setlocale(locale.LC_COLLATE, self.collation ) # We can't import datahandler stuff or we'll get a circular # dependency, so we rely on the available translations list @@ -763,14 +769,6 @@ class GrampsLocale(object): #ICU can digest strings and unicode return self.collator.getCollationKey(string).getByteArray() else: - try: - base_locale = locale.getlocale(locale.LC_COLLATE) - locale.setlocale(locale.LC_COLLATE, self.collation) - except Exception as err: - LOG.warn("Failed to set temporary locale with %s: %s", - self.lang, err) - return string - #locale in Python2 can't. try: if sys.version_info[0] < 3 and isinstance(string, unicode): key = locale.strxfrm(string.encode("utf-8", "replace")) @@ -781,11 +779,6 @@ class GrampsLocale(object): LOG.warn("Failed to obtain key for %s because %s", self.collation, str(err)) return string - try: - locale.setlocale(locale.LC_COLLATE, base_locale) - except Exception as err: - LOG.warn("Failed to restore locale %s", err) - return key return key def strcoll(self, string1, string2):