Repository: carltongibson/django-filter

Issue:
Add tests case for translation of OrderingFilter
Hello,

I observe that there is a lack of support for translation in OrderingFilter. I am unable to fix that now, but I prepared a tests case which might be helpful to fix that. 

```
$ tox -e py34-django19-restframework34
(…)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/django-filter/tests/test_filtering.py", line 1749, in test_ordering_translation_all_ovverride
    (u'-username', u'XYZXYZ')])
AssertionError: Lists differ: [('',[13 chars], ('username', 'BLABLA'), ('-username', 'BLABLA (descending)')] != [('',[13 chars], ('username', 'BLABLA'), ('-username', 'XYZXYZ')]

First differing element 2:
('-username', 'BLABLA (descending)')
('-username', 'XYZXYZ')

+ [('', '---------'), ('username', 'BLABLA'), ('-username', 'XYZXYZ')]
- [('', '---------'),
-  ('username', 'BLABLA'),
-  ('-username', 'BLABLA (descending)')]

======================================================================
FAIL: test_ordering_translation_default_label (tests.test_filtering.OrderingFilterTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/django-filter/tests/test_filtering.py", line 1707, in test_ordering_translation_default_label
    (u'-username', u'Nazwa użytkownika (malejący)')])
AssertionError: Lists differ: [('',[24 chars]e', 'Username'), ('-username', 'Username (descending)')] != [('',[24 chars]e', 'Nazwa użytkownika'), ('-username', 'Nazwa[21 chars]y)')]

First differing element 1:
('username', 'Username')
('username', 'Nazwa użytkownika')

  [('', '---------'),
-  ('username', 'Username'),
-  ('-username', 'Username (descending)')]
+  ('username', 'Nazwa użytkownika'),
+  ('-username', 'Nazwa użytkownika (malejący)')]

======================================================================
FAIL: test_ordering_translation_override_label (tests.test_filtering.OrderingFilterTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/django-filter/tests/test_filtering.py", line 1727, in test_ordering_translation_override_label
    (u'-username', u'BLABLA (malejący)')])
AssertionError: Lists differ: [('',[13 chars], ('username', 'BLABLA'), ('-username', 'BLABLA (descending)')] != [('',[13 chars], ('username', 'BLABLA'), ('-username', 'BLABLA (malejący)')]

First differing element 2:
('-username', 'BLABLA (descending)')
('-username', 'BLABLA (malejący)')

+ [('', '---------'), ('username', 'BLABLA'), ('-username', 'BLABLA (malejący)')]
- [('', '---------'),
-  ('username', 'BLABLA'),
-  ('-username', 'BLABLA (descending)')]
```


Discussion:

Resolving patch:
diff --git a/django_filters/filters.py b/django_filters/filters.py
--- a/django_filters/filters.py
+++ b/django_filters/filters.py
@@ -656,12 +656,12 @@ def normalize_fields(cls, fields):
 
     def build_choices(self, fields, labels):
         ascending = [
-            (param, labels.get(field, pretty_name(param)))
+            (param, labels.get(field, _(pretty_name(param))))
             for field, param in fields.items()
         ]
         descending = [
-            ('-%s' % pair[0], self.descending_fmt % pair[1])
-            for pair in ascending
+            ('-%s' % param, labels.get('-%s' % param, self.descending_fmt % label))
+            for param, label in ascending
         ]
 
         # interleave the ascending and descending choices


Test patch:
diff --git a/tests/settings.py b/tests/settings.py
--- a/tests/settings.py
+++ b/tests/settings.py
@@ -10,6 +10,7 @@
     'django.contrib.staticfiles',
     'django.contrib.auth',
     'rest_framework',
+    'django_filters',
     'tests.rest_framework',
     'tests',
 )
diff --git a/tests/test_filters.py b/tests/test_filters.py
--- a/tests/test_filters.py
+++ b/tests/test_filters.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
 from __future__ import absolute_import
 from __future__ import unicode_literals
 
@@ -8,6 +9,8 @@
 
 from django import forms
 from django.test import TestCase, override_settings
+from django.utils import translation
+from django.utils.translation import ugettext as _
 
 from django_filters import filters, widgets
 from django_filters.fields import (
@@ -1174,6 +1177,21 @@ def test_field_labels(self):
             ('-d', 'D (descending)'),
         ))
 
+    def test_field_labels_descending(self):
+        f = OrderingFilter(
+            fields=['username'],
+            field_labels={
+                'username': 'BLABLA',
+                '-username': 'XYZXYZ',
+            }
+        )
+
+        self.assertEqual(f.field.choices, [
+            ('', '---------'),
+            ('username', 'BLABLA'),
+            ('-username', 'XYZXYZ'),
+        ])
+
     def test_normalize_fields(self):
         f = OrderingFilter.normalize_fields
         O = OrderedDict
@@ -1213,3 +1231,31 @@ def test_widget(self):
 
         self.assertIsInstance(widget, widgets.BaseCSVWidget)
         self.assertIsInstance(widget, forms.Select)
+
+    def test_translation_sanity(self):
+        with translation.override('pl'):
+            self.assertEqual(_('Username'), 'Nazwa użytkownika')
+            self.assertEqual(_('%s (descending)') % _('Username'), 'Nazwa użytkownika (malejąco)')
+
+    def test_translation_default_label(self):
+        with translation.override('pl'):
+            f = OrderingFilter(fields=['username'])
+
+            self.assertEqual(f.field.choices, [
+                ('', '---------'),
+                ('username', 'Nazwa użytkownika'),
+                ('-username', 'Nazwa użytkownika (malejąco)'),
+            ])
+
+    def test_translation_override_label(self):
+        with translation.override('pl'):
+            f = OrderingFilter(
+                fields=['username'],
+                field_labels={'username': 'BLABLA'},
+            )
+
+            self.assertEqual(f.field.choices, [
+                ('', '---------'),
+                ('username', 'BLABLA'),
+                ('-username', 'BLABLA (malejąco)'),
+            ])