i18n: Set the correct language for translation in add_subscriptions_backend.

This commit is contained in:
Vishnu KS 2020-06-26 18:47:12 +05:30 committed by Tim Abbott
parent 28f5e86c7c
commit cc0b3a08c9
2 changed files with 36 additions and 26 deletions

View File

@ -3471,7 +3471,7 @@ class SubscriptionAPITest(ZulipTestCase):
principals=ujson.dumps([user1.id, user2.id]), principals=ujson.dumps([user1.id, user2.id]),
), ),
) )
self.assert_length(queries, 50) self.assert_length(queries, 51)
class GetStreamsTest(ZulipTestCase): class GetStreamsTest(ZulipTestCase):
def test_streams_api_for_bot_owners(self) -> None: def test_streams_api_for_bot_owners(self) -> None:

View File

@ -18,6 +18,7 @@ from django.conf import settings
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import transaction from django.db import transaction
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.utils.translation import override as override_language
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from zerver.decorator import ( from zerver.decorator import (
@ -384,17 +385,20 @@ def remove_subscriptions_backend(
return json_success(result) return json_success(result)
def you_were_just_subscribed_message(acting_user: UserProfile, def you_were_just_subscribed_message(acting_user: UserProfile,
recipient_user: UserProfile,
stream_names: Set[str]) -> str: stream_names: Set[str]) -> str:
subscriptions = sorted(list(stream_names)) subscriptions = sorted(list(stream_names))
if len(subscriptions) == 1: if len(subscriptions) == 1:
return _("@**{full_name}** subscribed you to the stream #**{stream_name}**.").format( with override_language(recipient_user.default_language):
full_name=acting_user.full_name, return _("@**{full_name}** subscribed you to the stream #**{stream_name}**.").format(
stream_name=subscriptions[0], full_name=acting_user.full_name,
) stream_name=subscriptions[0],
)
message = _("@**{full_name}** subscribed you to the following streams:").format( with override_language(recipient_user.default_language):
full_name=acting_user.full_name, message = _("@**{full_name}** subscribed you to the following streams:").format(
) full_name=acting_user.full_name,
)
message += "\n\n" message += "\n\n"
for stream_name in subscriptions: for stream_name in subscriptions:
message += f"* #**{stream_name}**\n" message += f"* #**{stream_name}**\n"
@ -509,33 +513,38 @@ def add_subscriptions_backend(
if not notify_stream_names: if not notify_stream_names:
continue continue
sender = get_system_bot(settings.NOTIFICATION_BOT)
recipient_user = email_to_user_profile[email]
msg = you_were_just_subscribed_message( msg = you_were_just_subscribed_message(
acting_user=user_profile, acting_user=user_profile,
recipient_user=recipient_user,
stream_names=notify_stream_names, stream_names=notify_stream_names,
) )
sender = get_system_bot(settings.NOTIFICATION_BOT)
notifications.append( notifications.append(
internal_prep_private_message( internal_prep_private_message(
realm=user_profile.realm, realm=user_profile.realm,
sender=sender, sender=sender,
recipient_user=email_to_user_profile[email], recipient_user=recipient_user,
content=msg)) content=msg))
if announce and len(created_streams) > 0: if announce and len(created_streams) > 0:
notifications_stream = user_profile.realm.get_notifications_stream() notifications_stream = user_profile.realm.get_notifications_stream()
if notifications_stream is not None: if notifications_stream is not None:
if len(created_streams) > 1: with override_language(notifications_stream.realm.default_language):
content = _("@_**%(user_name)s|%(user_id)d** created the following streams: %(stream_str)s.") if len(created_streams) > 1:
else: content = _("@_**%(user_name)s|%(user_id)d** created the following streams: %(stream_str)s.")
content = _("@_**%(user_name)s|%(user_id)d** created a new stream %(stream_str)s.") else:
content = _("@_**%(user_name)s|%(user_id)d** created a new stream %(stream_str)s.")
topic = _('new streams')
content = content % { content = content % {
'user_name': user_profile.full_name, 'user_name': user_profile.full_name,
'user_id': user_profile.id, 'user_id': user_profile.id,
'stream_str': ", ".join(f'#**{s.name}**' for s in created_streams)} 'stream_str': ", ".join(f'#**{s.name}**' for s in created_streams)}
sender = get_system_bot(settings.NOTIFICATION_BOT) sender = get_system_bot(settings.NOTIFICATION_BOT)
topic = _('new streams')
notifications.append( notifications.append(
internal_prep_stream_message( internal_prep_stream_message(
@ -550,18 +559,19 @@ def add_subscriptions_backend(
if not user_profile.realm.is_zephyr_mirror_realm and len(created_streams) > 0: if not user_profile.realm.is_zephyr_mirror_realm and len(created_streams) > 0:
sender = get_system_bot(settings.NOTIFICATION_BOT) sender = get_system_bot(settings.NOTIFICATION_BOT)
for stream in created_streams: for stream in created_streams:
notifications.append( with override_language(stream.realm.default_language):
internal_prep_stream_message( notifications.append(
realm=user_profile.realm, internal_prep_stream_message(
sender=sender, realm=user_profile.realm,
stream=stream, sender=sender,
topic=Realm.STREAM_EVENTS_NOTIFICATION_TOPIC, stream=stream,
content=_('Stream created by @_**{user_name}|{user_id}**.').format( topic=Realm.STREAM_EVENTS_NOTIFICATION_TOPIC,
user_name=user_profile.full_name, content=_('Stream created by @_**{user_name}|{user_id}**.').format(
user_id=user_profile.id, user_name=user_profile.full_name,
user_id=user_profile.id,
),
), ),
), )
)
if len(notifications) > 0: if len(notifications) > 0:
do_send_messages(notifications, mark_as_read=[user_profile.id]) do_send_messages(notifications, mark_as_read=[user_profile.id])