Skip to content

Commit 470b89c

Browse files
author
Carlton Gibson
authored
Merge pull request carltongibson#546 from rpkilby/fix-orderingfilter-translation
Fix OrderingFilter label translation
2 parents 4018596 + b63d222 commit 470b89c

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

django_filters/filters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,12 @@ def normalize_fields(cls, fields):
656656

657657
def build_choices(self, fields, labels):
658658
ascending = [
659-
(param, labels.get(field, pretty_name(param)))
659+
(param, labels.get(field, _(pretty_name(param))))
660660
for field, param in fields.items()
661661
]
662662
descending = [
663-
('-%s' % pair[0], self.descending_fmt % pair[1])
664-
for pair in ascending
663+
('-%s' % param, labels.get('-%s' % param, self.descending_fmt % label))
664+
for param, label in ascending
665665
]
666666

667667
# interleave the ascending and descending choices

tests/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'django.contrib.staticfiles',
1111
'django.contrib.auth',
1212
'rest_framework',
13+
'django_filters',
1314
'tests.rest_framework',
1415
'tests',
1516
)

tests/test_filters.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
from __future__ import absolute_import
23
from __future__ import unicode_literals
34

@@ -8,6 +9,8 @@
89

910
from django import forms
1011
from django.test import TestCase, override_settings
12+
from django.utils import translation
13+
from django.utils.translation import ugettext as _
1114

1215
from django_filters import filters, widgets
1316
from django_filters.fields import (
@@ -1174,6 +1177,21 @@ def test_field_labels(self):
11741177
('-d', 'D (descending)'),
11751178
))
11761179

1180+
def test_field_labels_descending(self):
1181+
f = OrderingFilter(
1182+
fields=['username'],
1183+
field_labels={
1184+
'username': 'BLABLA',
1185+
'-username': 'XYZXYZ',
1186+
}
1187+
)
1188+
1189+
self.assertEqual(f.field.choices, [
1190+
('', '---------'),
1191+
('username', 'BLABLA'),
1192+
('-username', 'XYZXYZ'),
1193+
])
1194+
11771195
def test_normalize_fields(self):
11781196
f = OrderingFilter.normalize_fields
11791197
O = OrderedDict
@@ -1213,3 +1231,31 @@ def test_widget(self):
12131231

12141232
self.assertIsInstance(widget, widgets.BaseCSVWidget)
12151233
self.assertIsInstance(widget, forms.Select)
1234+
1235+
def test_translation_sanity(self):
1236+
with translation.override('pl'):
1237+
self.assertEqual(_('Username'), 'Nazwa użytkownika')
1238+
self.assertEqual(_('%s (descending)') % _('Username'), 'Nazwa użytkownika (malejąco)')
1239+
1240+
def test_translation_default_label(self):
1241+
with translation.override('pl'):
1242+
f = OrderingFilter(fields=['username'])
1243+
1244+
self.assertEqual(f.field.choices, [
1245+
('', '---------'),
1246+
('username', 'Nazwa użytkownika'),
1247+
('-username', 'Nazwa użytkownika (malejąco)'),
1248+
])
1249+
1250+
def test_translation_override_label(self):
1251+
with translation.override('pl'):
1252+
f = OrderingFilter(
1253+
fields=['username'],
1254+
field_labels={'username': 'BLABLA'},
1255+
)
1256+
1257+
self.assertEqual(f.field.choices, [
1258+
('', '---------'),
1259+
('username', 'BLABLA'),
1260+
('-username', 'BLABLA (malejąco)'),
1261+
])

0 commit comments

Comments
 (0)