Bug 6538: Collation variants do not work properly

If ICU is available, inspect the environment for $COLLATION and on
a Mac also check the AppleCollationOrder default. Pass the results
to a special ICU Locale used for sorting.

(cherry picked from commit 49cdfe3283)
This commit is contained in:
John Ralls 2014-03-07 16:27:05 -08:00
parent 9766d81bb8
commit b299ac2e6e
2 changed files with 24 additions and 22 deletions

View File

@ -231,10 +231,10 @@ class GrampsLocale(object):
if not self.language:
self.language = [self.lang[:5]]
if 'LC_COLLATE' in os.environ:
coll = os.environ['LC_COLLATE']
if 'COLLATION' in os.environ:
coll = os.environ['COLLATION']
if HAVE_ICU:
if coll[:5] in ICU_LOCALES:
if coll[:2] in ICU_LOCALES:
self.collation = coll
else:
self.collation = self.lang
@ -341,6 +341,9 @@ class GrampsLocale(object):
else:
self.collation = self.lang
if HAVE_ICU and 'COLLATION' in os.environ:
self.collation = os.environ['COLLATION']
loc = locale.getlocale(locale.LC_NUMERIC)
if loc and loc[0]:
self.numeric = '.'.join(loc)
@ -538,9 +541,6 @@ class GrampsLocale(object):
else:
self.language = None
#For alternate collation sequences. Works only with ICU, and
#set only on Macs.
self.coll_qualifier = None
_first = self._GrampsLocale__first_instance
if self == _first:
self._GrampsLocale__init_first_instance()
@ -607,7 +607,7 @@ class GrampsLocale(object):
if not languages or len(languages) == 0:
LOG.warning("No language provided, using US English")
else:
raise ValueError("No usable translations in %s" %
raise ValueError("No usable translations in %s for " %
':'.join(languages))
translator = GrampsNullTranslations()
translator._language = "en"

View File

@ -194,24 +194,27 @@ def mac_setup_localization(glocale):
"""
Extract the collation (sort order) locale from the defaults string.
"""
apple_collation = _mac_get_gramps_defaults("Gramps", "AppleCollationOrder")
# The locale module can't deal with collation-qualified
# locales and setting one blows up setlocale, so we use
# $COLLATION directly instead.
if ('COLLATION') in os.environ:
apple_collation = os.environ['COLLATION']
else:
apple_collation = _mac_get_gramps_defaults("Gramps",
"AppleCollationOrder")
if not apple_collation:
apple_collation = _mac_get_gramps_defaults("Global",
"AppleCollationOrder")
if not apple_collation:
return (None, None)
print('No apple collation')
return None
apple_collation = apple_collation.strip()
if not apple_collation or apple_collation.startswith("root"):
return (None, None)
div = apple_collation.split(b"@")
collation = div[0]
qualifier = None
if len(div) > 1:
parts = div[1].split(b"=")
if len(parts) == 2 and parts[0] == 'collation':
qualifier = parts[1]
return (collation, qualifier)
print('No meaningful apple collation')
return None
print('Got collation %s from defaults' % apple_collation)
return apple_collation
#The action starts here
_locale = None
@ -234,12 +237,11 @@ def mac_setup_localization(glocale):
(glocale.lang, glocale.currency, glocale.calendar) = _mac_get_locale()
glocale.coll_qualifier = None
glocale.collation = locale.getlocale(locale.LC_COLLATE)[0]
if not glocale.collation:
(glocale.collation, glocale.coll_qualifier) = _mac_get_collation()
glocale.collation = _mac_get_collation()
if not glocale.lang and glocale.collation:
glocale.lang = glocale.check_available_translations(glocale.collation)
coll_parts = glocale.collation.split('@')
glocale.lang = glocale.check_available_translations(coll_parts[0])
glocale.lang = locale.normalize(glocale.lang)
glocale.encoding = glocale.lang.split('.')[1]