From f461a64a6bee8c9d9017dba6a4b64f34a44dfe8b Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 16 Oct 2020 18:42:50 -0700 Subject: [PATCH] i18n: Fix some ineffective calls to ugettext at top level. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Translation has no effect when we don’t yet know what language we’re translating for. Signed-off-by: Anders Kaseorg --- corporate/lib/stripe.py | 15 ++++++++------- corporate/views.py | 2 +- zerver/lib/exceptions.py | 24 ++++++++---------------- zerver/lib/integrations.py | 33 +++++++++++++++++---------------- zerver/views/user_settings.py | 7 ++++--- 5 files changed, 38 insertions(+), 43 deletions(-) diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index b8c5032f5a..c13b2ef270 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -15,6 +15,7 @@ from django.db import transaction from django.utils.timezone import now as timezone_now from django.utils.translation import override as override_language from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext_lazy from corporate.models import ( Customer, @@ -143,14 +144,14 @@ def get_idempotency_key(ledger_entry: LicenseLedger) -> Optional[str]: class BillingError(Exception): # error messages - CONTACT_SUPPORT = _("Something went wrong. Please contact {email}.").format( - email=settings.ZULIP_ADMINISTRATOR, - ) - TRY_RELOADING = _("Something went wrong. Please reload the page.") + CONTACT_SUPPORT = ugettext_lazy("Something went wrong. Please contact {email}.") + TRY_RELOADING = ugettext_lazy("Something went wrong. Please reload the page.") # description is used only for tests - def __init__(self, description: str, message: str=CONTACT_SUPPORT) -> None: + def __init__(self, description: str, message: Optional[str] = None) -> None: self.description = description + if message is None: + message = BillingError.CONTACT_SUPPORT.format(email=settings.ZULIP_ADMINISTRATOR) self.message = message class StripeCardError(BillingError): @@ -188,7 +189,7 @@ def catch_stripe_errors(func: CallableT) -> CallableT: raise StripeConnectionError( 'stripe connection error', _("Something went wrong. Please wait a few seconds and try again.")) - raise BillingError('other stripe error', BillingError.CONTACT_SUPPORT) + raise BillingError('other stripe error') return cast(CallableT, wrapped) @catch_stripe_errors @@ -373,7 +374,7 @@ def process_initial_upgrade(user: UserProfile, licenses: int, automanage_license billing_logger.warning( "Customer %s trying to upgrade, but has an active subscription", customer, ) - raise BillingError('subscribing with existing subscription', BillingError.TRY_RELOADING) + raise BillingError('subscribing with existing subscription', str(BillingError.TRY_RELOADING)) billing_cycle_anchor, next_invoice_date, period_end, price_per_license = compute_plan_parameters( automanage_licenses, billing_schedule, customer.default_discount, free_trial) diff --git a/corporate/views.py b/corporate/views.py index 913b4690a4..f6a30a4a09 100644 --- a/corporate/views.py +++ b/corporate/views.py @@ -142,7 +142,7 @@ def upgrade(request: HttpRequest, user: UserProfile, return json_error(e.message, data={'error_description': e.description}) except Exception: billing_logger.exception("Uncaught exception in billing:", stack_info=True) - error_message = BillingError.CONTACT_SUPPORT + error_message = BillingError.CONTACT_SUPPORT.format(email=settings.ZULIP_ADMINISTRATOR) error_description = "uncaught exception during upgrade" return json_error(error_message, data={'error_description': error_description}) else: diff --git a/zerver/lib/exceptions.py b/zerver/lib/exceptions.py index 20c84bd9eb..1a61b242a3 100644 --- a/zerver/lib/exceptions.py +++ b/zerver/lib/exceptions.py @@ -193,50 +193,42 @@ class InvalidJSONError(JsonableError): class OrganizationMemberRequired(JsonableError): code: ErrorCode = ErrorCode.UNAUTHORIZED_PRINCIPAL - MEMBER_REQUIRED_MESSAGE = _("Must be an organization member") - def __init__(self) -> None: - super().__init__(self.MEMBER_REQUIRED_MESSAGE) + pass @staticmethod def msg_format() -> str: - return OrganizationMemberRequired.MEMBER_REQUIRED_MESSAGE + return _("Must be an organization member") class OrganizationAdministratorRequired(JsonableError): code: ErrorCode = ErrorCode.UNAUTHORIZED_PRINCIPAL - ADMIN_REQUIRED_MESSAGE = _("Must be an organization administrator") - def __init__(self) -> None: - super().__init__(self.ADMIN_REQUIRED_MESSAGE) + pass @staticmethod def msg_format() -> str: - return OrganizationAdministratorRequired.ADMIN_REQUIRED_MESSAGE + return _("Must be an organization administrator") class OrganizationOwnerRequired(JsonableError): code: ErrorCode = ErrorCode.UNAUTHORIZED_PRINCIPAL - OWNER_REQUIRED_MESSAGE = _("Must be an organization owner") - def __init__(self) -> None: - super().__init__(self.OWNER_REQUIRED_MESSAGE) + pass @staticmethod def msg_format() -> str: - return OrganizationOwnerRequired.OWNER_REQUIRED_MESSAGE + return _("Must be an organization owner") class StreamAdministratorRequired(JsonableError): code: ErrorCode = ErrorCode.UNAUTHORIZED_PRINCIPAL - ADMIN_REQUIRED_MESSAGE = _("Must be an organization or stream administrator") - def __init__(self) -> None: - super().__init__(self.ADMIN_REQUIRED_MESSAGE) + pass @staticmethod def msg_format() -> str: - return StreamAdministratorRequired.ADMIN_REQUIRED_MESSAGE + return _("Must be an organization or stream administrator") class MarkdownRenderingException(Exception): pass diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.py index c57cc75b9f..2900ff57ce 100644 --- a/zerver/lib/integrations.py +++ b/zerver/lib/integrations.py @@ -5,8 +5,9 @@ from typing import Any, Dict, List, Optional, Sequence, Tuple from django.conf.urls import url from django.contrib.staticfiles.storage import staticfiles_storage from django.urls.resolvers import RegexPattern +from django.utils.functional import Promise from django.utils.module_loading import import_string -from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext as ugettext_lazy from zerver.lib.storage import static_path from zerver.lib.types import Validator @@ -30,21 +31,21 @@ Over time, we expect this registry to grow additional convenience features for writing and configuring integrations efficiently. """ -CATEGORIES: Dict[str, str] = { - 'meta-integration': _('Integration frameworks'), - 'continuous-integration': _('Continuous integration'), - 'customer-support': _('Customer support'), - 'deployment': _('Deployment'), - 'communication': _('Communication'), - 'financial': _('Financial'), - 'hr': _('HR'), - 'marketing': _('Marketing'), - 'misc': _('Miscellaneous'), - 'monitoring': _('Monitoring tools'), - 'project-management': _('Project management'), - 'productivity': _('Productivity'), - 'version-control': _('Version control'), - 'bots': _('Interactive bots'), +CATEGORIES: Dict[str, Promise] = { + 'meta-integration': ugettext_lazy('Integration frameworks'), + 'continuous-integration': ugettext_lazy('Continuous integration'), + 'customer-support': ugettext_lazy('Customer support'), + 'deployment': ugettext_lazy('Deployment'), + 'communication': ugettext_lazy('Communication'), + 'financial': ugettext_lazy('Financial'), + 'hr': ugettext_lazy('HR'), + 'marketing': ugettext_lazy('Marketing'), + 'misc': ugettext_lazy('Miscellaneous'), + 'monitoring': ugettext_lazy('Monitoring tools'), + 'project-management': ugettext_lazy('Project management'), + 'productivity': ugettext_lazy('Productivity'), + 'version-control': ugettext_lazy('Version control'), + 'bots': ugettext_lazy('Interactive bots'), } class Integration: diff --git a/zerver/views/user_settings.py b/zerver/views/user_settings.py index ec3425b3b0..d21149707d 100644 --- a/zerver/views/user_settings.py +++ b/zerver/views/user_settings.py @@ -8,6 +8,7 @@ from django.shortcuts import render from django.utils.html import escape from django.utils.safestring import SafeString from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext_lazy from confirmation.models import ( Confirmation, @@ -45,7 +46,7 @@ from zerver.lib.validator import check_bool, check_int, check_int_in, check_stri from zerver.models import UserProfile, avatar_changes_disabled, name_changes_disabled from zproject.backends import check_password_strength, email_belongs_to_ldap -AVATAR_CHANGES_DISABLED_ERROR = _("Avatar changes are disabled in this organization.") +AVATAR_CHANGES_DISABLED_ERROR = ugettext_lazy("Avatar changes are disabled in this organization.") def confirm_email_change(request: HttpRequest, confirmation_key: str) -> HttpResponse: try: @@ -242,7 +243,7 @@ def set_avatar_backend(request: HttpRequest, user_profile: UserProfile) -> HttpR return json_error(_("You must upload exactly one avatar.")) if avatar_changes_disabled(user_profile.realm) and not user_profile.is_realm_admin: - return json_error(AVATAR_CHANGES_DISABLED_ERROR) + return json_error(str(AVATAR_CHANGES_DISABLED_ERROR)) user_file = list(request.FILES.values())[0] if ((settings.MAX_AVATAR_FILE_SIZE * 1024 * 1024) < user_file.size): @@ -260,7 +261,7 @@ def set_avatar_backend(request: HttpRequest, user_profile: UserProfile) -> HttpR def delete_avatar_backend(request: HttpRequest, user_profile: UserProfile) -> HttpResponse: if avatar_changes_disabled(user_profile.realm) and not user_profile.is_realm_admin: - return json_error(AVATAR_CHANGES_DISABLED_ERROR) + return json_error(str(AVATAR_CHANGES_DISABLED_ERROR)) do_change_avatar_fields(user_profile, UserProfile.AVATAR_FROM_GRAVATAR, acting_user=user_profile) gravatar_url = avatar_url(user_profile)