[damned-lies] Sort team/languages according to current language collation (Fixes #535205)



commit 5513502b01849c521e7e2a53ea5e9e8924ff908b
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat Oct 31 16:34:17 2009 +0100

    Sort team/languages according to current language collation (Fixes #535205)
    
    Correct string sorting is not trivial to do with standard Python libs,
    as the locale module is not thread-safe and requires the corresponding
    locales be installed at the system level. We therefore use the PyICU
    module which is a wrapper around the ICU libraries.
    http://site.icu-project.org/

 README          |    1 +
 common/utils.py |   14 ++++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)
---
diff --git a/README b/README
index 1d111a7..c4e2bd5 100644
--- a/README
+++ b/README
@@ -41,6 +41,7 @@ Requirements
 6 - [Optional] python-openid and django-openid (see OpenID support
     below).
 
+7 - [Optional] python-pyicu for correct sorting in various languages
 
 Installation
 ============
diff --git a/common/utils.py b/common/utils.py
index 17220d5..eae87de 100644
--- a/common/utils.py
+++ b/common/utils.py
@@ -19,7 +19,13 @@
 # along with Damned Lies; if not, write to the Free Software Foundation, Inc.,
 # 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-from django.utils.translation import ugettext as _
+import operator
+from django.utils.translation import ugettext as _, get_language
+try:
+    import PyICU
+    pyicu_present = True
+except:
+    pyicu_present = False
 
 MIME_TYPES = {
     'json': 'application/json',
@@ -31,7 +37,11 @@ def trans_sort_object_list(lst, tr_field):
     for l in lst:
         l.translated_name = _(getattr(l, tr_field))
     templist = [(obj_.translated_name.lower(), obj_) for obj_ in lst]
-    templist.sort()
+    if pyicu_present:
+        collator = PyICU.Collator.createInstance(PyICU.Locale(get_language()))
+        templist.sort(key=operator.itemgetter(0), cmp=collator.compare)
+    else:
+        templist.sort()
     return [obj_ for (key1, obj_) in templist]
 
 def merge_sorted_by_field(object_list1, object_list2, field):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]