Changed get_locale_key to only use ICU if available.

This commit is contained in:
Tomas Groth 2014-03-11 20:38:47 +01:00
parent 2b55da02c1
commit 5ac6f853d0
1 changed files with 11 additions and 8 deletions

View File

@ -426,15 +426,18 @@ def get_locale_key(string):
The corresponding string. The corresponding string.
""" """
string = string.lower() string = string.lower()
# ICU is the prefered way to handle locale sort key, we fallback to locale.strxfrm which will work in most cases.
global ICU_COLLATOR global ICU_COLLATOR
if ICU_COLLATOR is None: try:
import icu if ICU_COLLATOR is None:
from .languagemanager import LanguageManager import icu
language = LanguageManager.get_language() from .languagemanager import LanguageManager
icu_locale = icu.Locale(language) language = LanguageManager.get_language()
ICU_COLLATOR = icu.Collator.createInstance(icu_locale) icu_locale = icu.Locale(language)
return ICU_COLLATOR.getSortKey(string) ICU_COLLATOR = icu.Collator.createInstance(icu_locale)
return ICU_COLLATOR.getSortKey(string)
except:
return locale.strxfrm(string).encode()
def get_natural_key(string): def get_natural_key(string):
""" """