From cc0b3a08c9d2127f8098612e34fa0a00542c38d4 Mon Sep 17 00:00:00 2001 From: Vishnu KS Date: Fri, 26 Jun 2020 18:47:12 +0530 Subject: [PATCH] i18n: Set the correct language for translation in add_subscriptions_backend. --- zerver/tests/test_subs.py | 2 +- zerver/views/streams.py | 60 +++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py index 7b98bb4fdf..0bcc3e0eaf 100644 --- a/zerver/tests/test_subs.py +++ b/zerver/tests/test_subs.py @@ -3471,7 +3471,7 @@ class SubscriptionAPITest(ZulipTestCase): principals=ujson.dumps([user1.id, user2.id]), ), ) - self.assert_length(queries, 50) + self.assert_length(queries, 51) class GetStreamsTest(ZulipTestCase): def test_streams_api_for_bot_owners(self) -> None: diff --git a/zerver/views/streams.py b/zerver/views/streams.py index 6deeb2991d..86b427de26 100644 --- a/zerver/views/streams.py +++ b/zerver/views/streams.py @@ -18,6 +18,7 @@ from django.conf import settings from django.core.exceptions import ValidationError from django.db import transaction from django.http import HttpRequest, HttpResponse +from django.utils.translation import override as override_language from django.utils.translation import ugettext as _ from zerver.decorator import ( @@ -384,17 +385,20 @@ def remove_subscriptions_backend( return json_success(result) def you_were_just_subscribed_message(acting_user: UserProfile, + recipient_user: UserProfile, stream_names: Set[str]) -> str: subscriptions = sorted(list(stream_names)) if len(subscriptions) == 1: - return _("@**{full_name}** subscribed you to the stream #**{stream_name}**.").format( - full_name=acting_user.full_name, - stream_name=subscriptions[0], - ) + with override_language(recipient_user.default_language): + return _("@**{full_name}** subscribed you to the stream #**{stream_name}**.").format( + full_name=acting_user.full_name, + stream_name=subscriptions[0], + ) - message = _("@**{full_name}** subscribed you to the following streams:").format( - full_name=acting_user.full_name, - ) + with override_language(recipient_user.default_language): + message = _("@**{full_name}** subscribed you to the following streams:").format( + full_name=acting_user.full_name, + ) message += "\n\n" for stream_name in subscriptions: message += f"* #**{stream_name}**\n" @@ -509,33 +513,38 @@ def add_subscriptions_backend( if not notify_stream_names: continue + sender = get_system_bot(settings.NOTIFICATION_BOT) + recipient_user = email_to_user_profile[email] + msg = you_were_just_subscribed_message( acting_user=user_profile, + recipient_user=recipient_user, stream_names=notify_stream_names, ) - sender = get_system_bot(settings.NOTIFICATION_BOT) notifications.append( internal_prep_private_message( realm=user_profile.realm, sender=sender, - recipient_user=email_to_user_profile[email], + recipient_user=recipient_user, content=msg)) if announce and len(created_streams) > 0: notifications_stream = user_profile.realm.get_notifications_stream() if notifications_stream is not None: - if len(created_streams) > 1: - content = _("@_**%(user_name)s|%(user_id)d** created the following streams: %(stream_str)s.") - else: - content = _("@_**%(user_name)s|%(user_id)d** created a new stream %(stream_str)s.") + with override_language(notifications_stream.realm.default_language): + if len(created_streams) > 1: + content = _("@_**%(user_name)s|%(user_id)d** created the following streams: %(stream_str)s.") + else: + content = _("@_**%(user_name)s|%(user_id)d** created a new stream %(stream_str)s.") + topic = _('new streams') + content = content % { 'user_name': user_profile.full_name, 'user_id': user_profile.id, 'stream_str': ", ".join(f'#**{s.name}**' for s in created_streams)} sender = get_system_bot(settings.NOTIFICATION_BOT) - topic = _('new streams') notifications.append( 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: sender = get_system_bot(settings.NOTIFICATION_BOT) for stream in created_streams: - notifications.append( - internal_prep_stream_message( - realm=user_profile.realm, - sender=sender, - stream=stream, - topic=Realm.STREAM_EVENTS_NOTIFICATION_TOPIC, - content=_('Stream created by @_**{user_name}|{user_id}**.').format( - user_name=user_profile.full_name, - user_id=user_profile.id, + with override_language(stream.realm.default_language): + notifications.append( + internal_prep_stream_message( + realm=user_profile.realm, + sender=sender, + stream=stream, + topic=Realm.STREAM_EVENTS_NOTIFICATION_TOPIC, + content=_('Stream created by @_**{user_name}|{user_id}**.').format( + user_name=user_profile.full_name, + user_id=user_profile.id, + ), ), - ), - ) + ) if len(notifications) > 0: do_send_messages(notifications, mark_as_read=[user_profile.id])