From d215cbd63ab4325cb632b7d570ae6827a0f139d3 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Wed, 23 Nov 2005 15:42:09 +0000 Subject: [PATCH] Added translation strings to django.contrib.comments. Taken from new-admin. git-svn-id: http://code.djangoproject.com/svn/django/trunk@1364 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/comments/models/comments.py | 84 +++++++++++-------- .../contrib/comments/templatetags/comments.py | 19 +++-- django/contrib/comments/views/comments.py | 30 +++---- django/contrib/comments/views/karma.py | 6 +- 4 files changed, 77 insertions(+), 62 deletions(-) diff --git a/django/contrib/comments/models/comments.py b/django/contrib/comments/models/comments.py index 33a44c8494..3062af62de 100644 --- a/django/contrib/comments/models/comments.py +++ b/django/contrib/comments/models/comments.py @@ -1,32 +1,35 @@ from django.core import meta from django.models import auth, core +from django.utils.translation import gettext_lazy as _ class Comment(meta.Model): user = meta.ForeignKey(auth.User, raw_id_admin=True) content_type = meta.ForeignKey(core.ContentType) - object_id = meta.IntegerField('object ID') - headline = meta.CharField(maxlength=255, blank=True) - comment = meta.TextField(maxlength=3000) - rating1 = meta.PositiveSmallIntegerField('rating #1', blank=True, null=True) - rating2 = meta.PositiveSmallIntegerField('rating #2', blank=True, null=True) - rating3 = meta.PositiveSmallIntegerField('rating #3', blank=True, null=True) - rating4 = meta.PositiveSmallIntegerField('rating #4', blank=True, null=True) - rating5 = meta.PositiveSmallIntegerField('rating #5', blank=True, null=True) - rating6 = meta.PositiveSmallIntegerField('rating #6', blank=True, null=True) - rating7 = meta.PositiveSmallIntegerField('rating #7', blank=True, null=True) - rating8 = meta.PositiveSmallIntegerField('rating #8', blank=True, null=True) + object_id = meta.IntegerField(_('object ID')) + headline = meta.CharField(_('headline'), maxlength=255, blank=True) + comment = meta.TextField(_('comment'), maxlength=3000) + rating1 = meta.PositiveSmallIntegerField(_('rating #1'), blank=True, null=True) + rating2 = meta.PositiveSmallIntegerField(_('rating #2'), blank=True, null=True) + rating3 = meta.PositiveSmallIntegerField(_('rating #3'), blank=True, null=True) + rating4 = meta.PositiveSmallIntegerField(_('rating #4'), blank=True, null=True) + rating5 = meta.PositiveSmallIntegerField(_('rating #5'), blank=True, null=True) + rating6 = meta.PositiveSmallIntegerField(_('rating #6'), blank=True, null=True) + rating7 = meta.PositiveSmallIntegerField(_('rating #7'), blank=True, null=True) + rating8 = meta.PositiveSmallIntegerField(_('rating #8'), blank=True, null=True) # This field designates whether to use this row's ratings in aggregate # functions (summaries). We need this because people are allowed to post # multiple reviews on the same thing, but the system will only use the # latest one (with valid_rating=True) in tallying the reviews. - valid_rating = meta.BooleanField('is valid rating') - submit_date = meta.DateTimeField('date/time submitted', auto_now_add=True) - is_public = meta.BooleanField() - ip_address = meta.IPAddressField('IP address', blank=True, null=True) - is_removed = meta.BooleanField(help_text='Check this box if the comment is inappropriate. A "This comment has been removed" message will be displayed instead.') + valid_rating = meta.BooleanField(_('is valid rating')) + submit_date = meta.DateTimeField(_('date/time submitted'), auto_now_add=True) + is_public = meta.BooleanField(_('is public')) + ip_address = meta.IPAddressField(_('IP address'), blank=True, null=True) + is_removed = meta.BooleanField(_('is removed'), help_text=_('Check this box if the comment is inappropriate. A "This comment has been removed" message will be displayed instead.')) site = meta.ForeignKey(core.Site) class META: db_table = 'comments' + verbose_name = _('Comment') + verbose_name_plural = _('Comments') module_constants = { # min. and max. allowed dimensions for photo resizing (in pixels) 'MIN_PHOTO_DIMENSION': 5, @@ -79,7 +82,7 @@ class Comment(meta.Model): except ObjectDoesNotExist: return None - get_content_object.short_description = 'Content object' + get_content_object.short_description = _('Content object') def _fill_karma_cache(self): "Helper function that populates good/bad karma caches" @@ -107,9 +110,9 @@ class Comment(meta.Model): return self._karma_total_good + self._karma_total_bad def get_as_text(self): - return 'Posted by %s at %s\n\n%s\n\nhttp://%s%s' % \ - (self.get_user().username, self.submit_date, - self.comment, self.get_site().domain, self.get_absolute_url()) + return _('Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % \ + {'user': self.get_user().username, 'date': self.submit_date, + 'comment': self.comment, 'domain': self.get_site().domain, 'url': self.get_absolute_url()} def _module_get_security_hash(options, photo_options, rating_options, target): """ @@ -155,17 +158,19 @@ class Comment(meta.Model): class FreeComment(meta.Model): # A FreeComment is a comment by a non-registered user. content_type = meta.ForeignKey(core.ContentType) - object_id = meta.IntegerField('object ID') - comment = meta.TextField(maxlength=3000) - person_name = meta.CharField("person's name", maxlength=50) - submit_date = meta.DateTimeField('date/time submitted', auto_now_add=True) - is_public = meta.BooleanField() - ip_address = meta.IPAddressField() + object_id = meta.IntegerField(_('object ID')) + comment = meta.TextField(_('comment'), maxlength=3000) + person_name = meta.CharField(_("person's name"), maxlength=50) + submit_date = meta.DateTimeField(_('date/time submitted'), auto_now_add=True) + is_public = meta.BooleanField(_('is public')) + ip_address = meta.IPAddressField(_('ip address')) # TODO: Change this to is_removed, like Comment - approved = meta.BooleanField('approved by staff') + approved = meta.BooleanField(_('approved by staff')) site = meta.ForeignKey(core.Site) class META: db_table = 'comments_free' + verbose_name = _('Free comment') + verbose_name_plural = _('Free comments') ordering = ('-submit_date',) admin = meta.Admin( fields = ( @@ -196,15 +201,17 @@ class FreeComment(meta.Model): except ObjectDoesNotExist: return None - get_content_object.short_description = 'Content object' + get_content_object.short_description = _('Content object') class KarmaScore(meta.Model): user = meta.ForeignKey(auth.User) comment = meta.ForeignKey(Comment) - score = meta.SmallIntegerField(db_index=True) - scored_date = meta.DateTimeField(auto_now=True) + score = meta.SmallIntegerField(_('score'), db_index=True) + scored_date = meta.DateTimeField(_('score date'), auto_now=True) class META: module_name = 'karma' + verbose_name = _('Karma score') + verbose_name_plural = _('Karma scores') unique_together = (('user', 'comment'),) module_constants = { # what users get if they don't have any karma @@ -213,7 +220,7 @@ class KarmaScore(meta.Model): } def __repr__(self): - return "%d rating by %s" % (self.score, self.get_user()) + return _("%(score)d rating by %(user)s") % {'score': self.score, 'user': self.get_user()} def _module_vote(user_id, comment_id, score): try: @@ -238,13 +245,15 @@ class KarmaScore(meta.Model): class UserFlag(meta.Model): user = meta.ForeignKey(auth.User) comment = meta.ForeignKey(Comment) - flag_date = meta.DateTimeField(auto_now_add=True) + flag_date = meta.DateTimeField(_('flag date'), auto_now_add=True) class META: db_table = 'comments_user_flags' + verbose_name = _('User flag') + verbose_name_plural = _('User flags') unique_together = (('user', 'comment'),) def __repr__(self): - return "Flag by %r" % self.get_user() + return _("Flag by %r") % self.get_user() def _module_flag(comment, user): """ @@ -259,17 +268,20 @@ class UserFlag(meta.Model): except UserFlagDoesNotExist: from django.core.mail import mail_managers f = UserFlag(None, user.id, comment.id, None) - message = 'This comment was flagged by %s:\n\n%s' % (user.username, comment.get_as_text()) + message = _('This comment was flagged by %(user)s:\n\n%(text)s') % {'user': user.username, 'text': comment.get_as_text()} mail_managers('Comment flagged', message, fail_silently=True) f.save() class ModeratorDeletion(meta.Model): user = meta.ForeignKey(auth.User, verbose_name='moderator') comment = meta.ForeignKey(Comment) - deletion_date = meta.DateTimeField(auto_now_add=True) + deletion_date = meta.DateTimeField(_('deletion date'), auto_now_add=True) class META: db_table = 'comments_moderator_deletions' + verbose_name = _('Moderator deletion') + verbose_name_plural = _('Moderator deletions') unique_together = (('user', 'comment'),) def __repr__(self): - return "Moderator deletion by %r" % self.get_user() + return _("Moderator deletion by %r") % self.get_user() + diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py index b6f57a652b..72c586e206 100644 --- a/django/contrib/comments/templatetags/comments.py +++ b/django/contrib/comments/templatetags/comments.py @@ -7,17 +7,18 @@ from django.models.core import contenttypes import re COMMENT_FORM = ''' +{% load i18n %} {% if display_form %}
{% if user.is_anonymous %} -

Username:
Password: (Forgotten your password?)

+

{% trans "Username:" %}
{% trans "Password:" %} ({% trans "Forgotten your password?" %})

{% else %} -

Username: {{ user.username }} (Log out)

+

{% trans "Username:" %} {{ user.username }} ({% trans "Log out" %})

{% endif %} {% if ratings_optional or ratings_required %} -

Ratings ({% if ratings_required %}Required{% else %}Optional{% endif %}):

+

{% trans "Ratings" %} ({% if ratings_required %}{% trans "Required" %}{% else %}{% trans "Optional" %}{% endif %}):

{% for value in rating_range %}{% endfor %} {% for rating in rating_choices %} @@ -28,16 +29,16 @@ COMMENT_FORM = ''' {% endif %} {% if photos_optional or photos_required %} -

Post a photo ({% if photos_required %}Required{% else %}Optional{% endif %}):

+

{% trans "Post a photo" %} ({% if photos_required %}{% trans "Required" %}{% else %}{% trans "Optional" %}{% endif %}):

{% endif %} -

Comment:

+

{% trans "Comment:" %}

-

+

{% endif %} ''' @@ -45,12 +46,12 @@ COMMENT_FORM = ''' FREE_COMMENT_FORM = ''' {% if display_form %} -

Your name:

-

Comment:

+

{% trans "Your name:" %}

+

{% trans "Comment:" %}

-

+

{% endif %} ''' diff --git a/django/contrib/comments/views/comments.py b/django/contrib/comments/views/comments.py index 996f3d0328..5918db7dc8 100644 --- a/django/contrib/comments/views/comments.py +++ b/django/contrib/comments/views/comments.py @@ -9,6 +9,7 @@ from django.parts.auth.formfields import AuthenticationForm from django.utils.httpwrappers import HttpResponseRedirect from django.utils.text import normalize_newlines from django.conf.settings import BANNED_IPS, COMMENTS_ALLOW_PROFANITIES, COMMENTS_SKETCHY_USERS_GROUP, COMMENTS_FIRST_FEW, SITE_ID +from django.utils.translation import ngettext import base64, datetime COMMENTS_PER_PAGE = 20 @@ -21,7 +22,7 @@ class PublicCommentManipulator(AuthenticationForm): choices = [(c, c) for c in ratings_range] def get_validator_list(rating_num): if rating_num <= num_rating_choices: - return [validators.RequiredIfOtherFieldsGiven(['rating%d' % i for i in range(1, 9) if i != rating_num], "This rating is required because you've entered at least one other rating.")] + return [validators.RequiredIfOtherFieldsGiven(['rating%d' % i for i in range(1, 9) if i != rating_num], _("This rating is required because you've entered at least one other rating."))] else: return [] self.fields.extend([ @@ -105,11 +106,12 @@ class PublicCommentManipulator(AuthenticationForm): # If the commentor has posted fewer than COMMENTS_FIRST_FEW comments, # send the comment to the managers. if self.user_cache.get_comments_comment_count() <= COMMENTS_FIRST_FEW: - message = 'This comment was posted by a user who has posted fewer than %s comments:\n\n%s' % \ - (COMMENTS_FIRST_FEW, c.get_as_text()) + message = ngettext('This comment was posted by a user who has posted fewer than %(count)s comment:\n\n%(text)s', + 'This comment was posted by a user who has posted fewer than %(count)s comments:\n\n%(text)s') % \ + {'count': COMMENTS_FIRST_FEW, 'text': c.get_as_text()} mail_managers("Comment posted by rookie user", message) if COMMENTS_SKETCHY_USERS_GROUP and COMMENTS_SKETCHY_USERS_GROUP in [g.id for g in self.user_cache.get_group_list()]: - message = 'This comment was posted by a sketchy user:\n\n%s' % c.get_as_text() + message = _('This comment was posted by a sketchy user:\n\n%(text)s') % {'text': c.get_as_text()} mail_managers("Comment posted by sketchy user (%s)" % self.user_cache.username, c.get_as_text()) return c @@ -181,15 +183,15 @@ def post_comment(request): choice of ratings """ if not request.POST: - raise Http404, "Only POSTs are allowed" + raise Http404, _("Only POSTs are allowed") try: options, target, security_hash = request.POST['options'], request.POST['target'], request.POST['gonzo'] except KeyError: - raise Http404, "One or more of the required fields wasn't submitted" + raise Http404, _("One or more of the required fields wasn't submitted") photo_options = request.POST.get('photo_options', '') rating_options = normalize_newlines(request.POST.get('rating_options', '')) if comments.get_security_hash(options, photo_options, rating_options, target) != security_hash: - raise Http404, "Somebody tampered with the comment form (security violation)" + raise Http404, _("Somebody tampered with the comment form (security violation)") # Now we can be assured the data is valid. if rating_options: rating_range, rating_choices = comments.get_rating_options(base64.decodestring(rating_options)) @@ -199,7 +201,7 @@ def post_comment(request): try: obj = contenttypes.get_object(pk=content_type_id).get_object_for_this_type(pk=object_id) except ObjectDoesNotExist: - raise Http404, "The comment form had an invalid 'target' parameter -- the object ID was invalid" + raise Http404, _("The comment form had an invalid 'target' parameter -- the object ID was invalid") option_list = options.split(',') # options is something like 'pa,ra' new_data = request.POST.copy() new_data['content_type_id'] = content_type_id @@ -249,7 +251,7 @@ def post_comment(request): comment = manipulator.save(new_data) return HttpResponseRedirect("/comments/posted/?c=%s:%s" % (content_type_id, object_id)) else: - raise Http404, "The comment form didn't provide either 'preview' or 'post'" + raise Http404, _("The comment form didn't provide either 'preview' or 'post'") def post_free_comment(request): """ @@ -272,19 +274,19 @@ def post_free_comment(request): post a comment). """ if not request.POST: - raise Http404, "Only POSTs are allowed" + raise Http404, _("Only POSTs are allowed") try: options, target, security_hash = request.POST['options'], request.POST['target'], request.POST['gonzo'] except KeyError: - raise Http404, "One or more of the required fields wasn't submitted" + raise Http404, _("One or more of the required fields wasn't submitted") if comments.get_security_hash(options, '', '', target) != security_hash: - raise Http404, "Somebody tampered with the comment form (security violation)" + raise Http404, _("Somebody tampered with the comment form (security violation)") content_type_id, object_id = target.split(':') # target is something like '52:5157' content_type = contenttypes.get_object(pk=content_type_id) try: obj = content_type.get_object_for_this_type(pk=object_id) except ObjectDoesNotExist: - raise Http404, "The comment form had an invalid 'target' parameter -- the object ID was invalid" + raise Http404, _("The comment form had an invalid 'target' parameter -- the object ID was invalid") option_list = options.split(',') new_data = request.POST.copy() new_data['content_type_id'] = content_type_id @@ -313,7 +315,7 @@ def post_free_comment(request): comment = manipulator.save(new_data) return HttpResponseRedirect("/comments/posted/?c=%s:%s" % (content_type_id, object_id)) else: - raise Http404, "The comment form didn't provide either 'preview' or 'post'" + raise Http404, _("The comment form didn't provide either 'preview' or 'post'") def comment_was_posted(request): """ diff --git a/django/contrib/comments/views/karma.py b/django/contrib/comments/views/karma.py index 2e2b2495ab..9db68d69f1 100644 --- a/django/contrib/comments/views/karma.py +++ b/django/contrib/comments/views/karma.py @@ -15,13 +15,13 @@ def vote(request, comment_id, vote): if not rating: raise Http404, "Invalid vote" if request.user.is_anonymous(): - raise Http404, "Anonymous users cannot vote" + raise Http404, _("Anonymous users cannot vote") try: comment = comments.get_object(pk=comment_id) except comments.CommentDoesNotExist: - raise Http404, "Invalid comment ID" + raise Http404, _("Invalid comment ID") if comment.user_id == request.user.id: - raise Http404, "No voting for yourself" + raise Http404, _("No voting for yourself") karma.vote(request.user.id, comment_id, rating) # Reload comment to ensure we have up to date karma count comment = comments.get_object(pk=comment_id)
 {{ value }}