Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of new ungettext_lazy function at appropriate places. #702

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions django/contrib/comments/admin.py
Expand Up @@ -3,7 +3,7 @@
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.comments.models import Comment
from django.utils.translation import ugettext_lazy as _, ungettext
from django.utils.translation import ugettext_lazy as _, ungettext, ungettext_lazy
from django.contrib.comments import get_model
from django.contrib.comments.views.moderation import perform_flag, perform_approve, perform_delete

Expand Down Expand Up @@ -52,17 +52,20 @@ def get_actions(self, request):

def flag_comments(self, request, queryset):
self._bulk_flag(request, queryset, perform_flag,
lambda n: ungettext('flagged', 'flagged', n))
ungettext_lazy('%d comment was successfully flagged',
'%d comments were successfully flagged'))
flag_comments.short_description = _("Flag selected comments")

def approve_comments(self, request, queryset):
self._bulk_flag(request, queryset, perform_approve,
lambda n: ungettext('approved', 'approved', n))
ungettext_lazy('%d comment was successfully approved',
'%d comments were successfully approved'))
approve_comments.short_description = _("Approve selected comments")

def remove_comments(self, request, queryset):
self._bulk_flag(request, queryset, perform_delete,
lambda n: ungettext('removed', 'removed', n))
ungettext_lazy('%d comment was successfully removed',
'%d comments were successfully removed'))
remove_comments.short_description = _("Remove selected comments")

def _bulk_flag(self, request, queryset, action, done_message):
Expand All @@ -75,10 +78,7 @@ def _bulk_flag(self, request, queryset, action, done_message):
action(request, comment)
n_comments += 1

msg = ungettext('1 comment was successfully %(action)s.',
'%(count)s comments were successfully %(action)s.',
n_comments)
self.message_user(request, msg % {'count': n_comments, 'action': done_message(n_comments)})
self.message_user(request, done_message % n_comments)

# Only register the default admin if the model is the built-in comment model
# (this won't be true if there's a custom comment app).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we fix this, we should do it right, because currently, it is awfully wrong, i18n-wise. The proper way is to duplicate the strings in each flag_* methods ('%(count)s comments were successfully flagged.', '%(count)s comments were successfully approved.', ...).

Expand Down
22 changes: 11 additions & 11 deletions django/utils/timesince.py
Expand Up @@ -3,7 +3,7 @@
import datetime

from django.utils.timezone import is_aware, utc
from django.utils.translation import ungettext, ugettext
from django.utils.translation import ugettext, ungettext_lazy

def timesince(d, now=None, reversed=False):
"""
Expand All @@ -19,12 +19,12 @@ def timesince(d, now=None, reversed=False):
Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
"""
chunks = (
(60 * 60 * 24 * 365, lambda n: ungettext('year', 'years', n)),
(60 * 60 * 24 * 30, lambda n: ungettext('month', 'months', n)),
(60 * 60 * 24 * 7, lambda n : ungettext('week', 'weeks', n)),
(60 * 60 * 24, lambda n : ungettext('day', 'days', n)),
(60 * 60, lambda n: ungettext('hour', 'hours', n)),
(60, lambda n: ungettext('minute', 'minutes', n))
(60 * 60 * 24 * 365, ungettext_lazy('%d year', '%d years')),
(60 * 60 * 24 * 30, ungettext_lazy('%d month', '%d months')),
(60 * 60 * 24 * 7, ungettext_lazy('%d week', '%d weeks')),
(60 * 60 * 24, ungettext_lazy('%d day', '%d days')),
(60 * 60, ungettext_lazy('%d hour', '%d hours')),
(60, ungettext_lazy('%d minute', '%d minutes'))
)
# Convert datetime.date to datetime.datetime for comparison.
if not isinstance(d, datetime.datetime):
Expand All @@ -40,19 +40,19 @@ def timesince(d, now=None, reversed=False):
since = delta.days * 24 * 60 * 60 + delta.seconds
if since <= 0:
# d is in the future compared to now, stop processing.
return '0 ' + ugettext('minutes')
return ugettext('0 minutes')
for i, (seconds, name) in enumerate(chunks):
count = since // seconds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also fix this awful return '0 ' + ugettext('minutes'), just above?

if count != 0:
break
s = ugettext('%(number)d %(type)s') % {'number': count, 'type': name(count)}
result = name % count
if i + 1 < len(chunks):
# Now get the second item
seconds2, name2 = chunks[i + 1]
count2 = (since - (seconds * count)) // seconds2
if count2 != 0:
s += ugettext(', %(number)d %(type)s') % {'number': count2, 'type': name2(count2)}
return s
result += ugettext(', ') + name2 % count2
return result

def timeuntil(d, now=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have put the strings in a list, then finally ugettext(", ").join(the_list)

"""
Expand Down
29 changes: 27 additions & 2 deletions tests/regressiontests/comment_tests/tests/moderation_view_tests.py
@@ -1,9 +1,9 @@
from __future__ import absolute_import

from __future__ import absolute_import, unicode_literals
from django.contrib.auth.models import User, Permission
from django.contrib.comments import signals
from django.contrib.comments.models import Comment, CommentFlag
from django.contrib.contenttypes.models import ContentType
from django.utils import translation

from . import CommentTestCase

Expand Down Expand Up @@ -281,3 +281,28 @@ def testActionsDisabledDelete(self):
response = self.client.get('/admin2/comments/comment/')
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, '<option value="delete_selected">')

def performActionAndCheckMessage(self, action, action_params, expected_message):
response = self.client.post('/admin/comments/comment/',
data={'_selected_action': action_params,
'action': action,
'index': 0},
follow=True)
self.assertContains(response, expected_message)

def testActionsMessageTranslations(self):
c1, c2, c3, c4 = self.createSomeComments()
one_comment = c1.pk
many_comments = [c2.pk, c3.pk, c4.pk]
makeModerator("normaluser")
self.client.login(username="normaluser", password="normaluser")
with translation.override('en'):
#Test approving
self.performActionAndCheckMessage('approve_comments', one_comment, '1 comment was successfully approved')
self.performActionAndCheckMessage('approve_comments', many_comments, '3 comments were successfully approved')
#Test flagging
self.performActionAndCheckMessage('flag_comments', one_comment, '1 comment was successfully flagged')
self.performActionAndCheckMessage('flag_comments', many_comments, '3 comments were successfully flagged')
#Test removing
self.performActionAndCheckMessage('remove_comments', one_comment, '1 comment was successfully removed')
self.performActionAndCheckMessage('remove_comments', many_comments, '3 comments were successfully removed')