diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index 3529884d3..cdda7b3ea 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -376,16 +376,21 @@ def format_time(text, local_time): def get_locale_key(string): """ Creates a key for case insensitive, locale aware string sorting. + + ``string`` + The corresponding string. """ string = string.lower() # For Python 3 on platforms other than Windows ICU is not necessary. In those cases locale.strxfrm(str) can be used. - global ICU_COLLATOR - if ICU_COLLATOR is None: - from languagemanager import LanguageManager - locale = LanguageManager.get_language() - icu_locale = icu.Locale(locale) - ICU_COLLATOR = icu.Collator.createInstance(icu_locale) - return ICU_COLLATOR.getSortKey(string) + if os.name == 'nt': + global ICU_COLLATOR + if ICU_COLLATOR is None: + from .languagemanager import LanguageManager + language = LanguageManager.get_language() + icu_locale = icu.Locale(language) + ICU_COLLATOR = icu.Collator.createInstance(icu_locale) + return ICU_COLLATOR.getSortKey(string) + return locale.strxfrm(string).encode() def get_natural_key(string): diff --git a/tests/functional/openlp_core_utils/test_utils.py b/tests/functional/openlp_core_utils/test_utils.py index 8e3a427ed..9c8db3ac9 100644 --- a/tests/functional/openlp_core_utils/test_utils.py +++ b/tests/functional/openlp_core_utils/test_utils.py @@ -105,14 +105,33 @@ class TestUtils(TestCase): # THEN: The file name should be cleaned. assert result == wanted_name, u'The file name should not contain any special characters.' - def get_locale_key_test(self): + def get_locale_key_windows_test(self): """ Test the get_locale_key(string) function """ - with patch(u'openlp.core.utils.languagemanager.LanguageManager.get_language') as mocked_get_language: + with patch(u'openlp.core.utils.languagemanager.LanguageManager.get_language') as mocked_get_language, \ + patch(u'openlp.core.utils.os') as mocked_os: # GIVEN: The language is German # 0x00C3 (A with diaresis) should be sorted as "A". 0x00DF (sharp s) should be sorted as "ss". mocked_get_language.return_value = u'de' + mocked_os.name = u'nt' + unsorted_list = [u'Auszug', u'Aushang', u'\u00C4u\u00DFerung'] + # WHEN: We sort the list and use get_locale_key() to generate the sorting keys + # THEN: We get a properly sorted list + test_passes = sorted(unsorted_list, key=get_locale_key) == [u'Aushang', u'\u00C4u\u00DFerung', u'Auszug'] + assert test_passes, u'Strings should be sorted properly' + + def get_locale_key_linux_test(self): + + """ + Test the get_locale_key(string) function + """ + with patch(u'openlp.core.utils.languagemanager.LanguageManager.get_language') as mocked_get_language, \ + patch(u'openlp.core.utils.os.name') as mocked_os: + # GIVEN: The language is German + # 0x00C3 (A with diaresis) should be sorted as "A". 0x00DF (sharp s) should be sorted as "ss". + mocked_get_language.return_value = u'de' + mocked_os.name = u'linux' unsorted_list = [u'Auszug', u'Aushang', u'\u00C4u\u00DFerung'] # WHEN: We sort the list and use get_locale_key() to generate the sorting keys # THEN: We get a properly sorted list