From eb9466f346f5c9901ef62a8c8fff20f317286f17 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sat, 8 Jun 2024 10:46:47 +0100 Subject: [PATCH] Add separate translations for each content type Fixes #355 Fixes #538 --- app/blueprints/packages/packages.py | 2 +- app/blueprints/users/profile.py | 8 +-- app/models/packages.py | 27 ++++++++ app/models/users.py | 91 ++++++++++++++++++++----- app/querybuilder.py | 4 ++ app/templates/base.html | 2 +- app/templates/emails/notification.html | 2 +- app/templates/packages/create_edit.html | 2 +- app/templates/users/settings_email.html | 4 +- 9 files changed, 113 insertions(+), 29 deletions(-) diff --git a/app/blueprints/packages/packages.py b/app/blueprints/packages/packages.py index 37136fc9..82282ec4 100644 --- a/app/blueprints/packages/packages.py +++ b/app/blueprints/packages/packages.py @@ -111,7 +111,7 @@ def list_all(): selected_tags = set(qb.tags) return render_template("packages/list.html", - query_hint=title, packages=query.items, pagination=query, + query_hint=qb.query_hint, packages=query.items, pagination=query, query=search, tags=tags, selected_tags=selected_tags, type=type_name, authors=authors, packages_count=query.total, topics=topics, noindex=qb.noindex) diff --git a/app/blueprints/users/profile.py b/app/blueprints/users/profile.py index 1a327ead..6ed1b1d2 100644 --- a/app/blueprints/users/profile.py +++ b/app/blueprints/users/profile.py @@ -162,10 +162,7 @@ def get_user_medals(user: User) -> Tuple[List[Medal], List[Medal]]: if user_package_ranks: top_rank = user_package_ranks[2] top_type = PackageType.coerce(user_package_ranks[0]) - if top_rank == 1: - title = gettext(u"Top %(type)s", type=top_type.text.lower()) - else: - title = gettext(u"Top %(group)d %(type)s", group=top_rank, type=top_type.text.lower()) + title = top_type.get_top_ordinal(top_rank) if top_type == PackageType.MOD: icon = "fa-box" elif top_type == PackageType.GAME: @@ -173,8 +170,7 @@ def get_user_medals(user: User) -> Tuple[List[Medal], List[Medal]]: else: icon = "fa-paint-brush" - description = gettext(u"%(display_name)s has a %(type)s placed at #%(place)d.", - display_name=user.display_name, type=top_type.text.lower(), place=top_rank) + description = top_type.get_top_ordinal_description(user.display_name, top_rank) unlocked.append( Medal.make_unlocked(place_to_color(top_rank), icon, title, description)) diff --git a/app/models/packages.py b/app/models/packages.py index 59990f4a..e5ffe02e 100644 --- a/app/models/packages.py +++ b/app/models/packages.py @@ -81,6 +81,33 @@ class PackageType(enum.Enum): elif self == PackageType.TXP: return lazy_gettext("Texture Packs") + def get_top_ordinal(self, place: int): + if place == 1: + if self == PackageType.MOD: + return lazy_gettext("Top mod") + elif self == PackageType.GAME: + return lazy_gettext("Top game") + elif self == PackageType.TXP: + return lazy_gettext("Top texture pack") + else: + if self == PackageType.MOD: + return lazy_gettext("Top %(place)d mod", place=place) + elif self == PackageType.GAME: + return lazy_gettext("Top %(place)d game", place=place) + elif self == PackageType.TXP: + return lazy_gettext("Top %(place)d texture pack", place=place) + + def get_top_ordinal_description(self, display_name: str, place: int): + if self == PackageType.MOD: + return lazy_gettext(u"%(display_name)s has a mod placed at #%(place)d.", + display_name=display_name, place=place) + elif self == PackageType.GAME: + return lazy_gettext(u"%(display_name)s has a game placed at #%(place)d.", + display_name=display_name, place=place) + elif self == PackageType.TXP: + return lazy_gettext(u"%(display_name)s has a texture pack placed at #%(place)d.", + display_name=display_name, place=place) + @classmethod def get(cls, name): try: diff --git a/app/models/users.py b/app/models/users.py index 2c6dfd25..37646283 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -17,7 +17,7 @@ import datetime import enum -from flask import url_for +from flask_babel import lazy_gettext from flask_login import UserMixin from sqlalchemy import desc, text @@ -400,36 +400,93 @@ class NotificationType(enum.Enum): # Any other OTHER = 0 - - def get_title(self): - return self.name.replace("_", " ").title() + @property + def title(self): + if self == NotificationType.PACKAGE_EDIT: + # NOTE: PACKAGE_EDIT notification type + return lazy_gettext("Package Edit") + elif self == NotificationType.PACKAGE_APPROVAL: + # NOTE: PACKAGE_APPROVAL notification type + return lazy_gettext("Package Approval") + elif self == NotificationType.NEW_THREAD: + # NOTE: NEW_THREAD notification type + return lazy_gettext("New Thread") + elif self == NotificationType.NEW_REVIEW: + # NOTE: NEW_REVIEW notification type + return lazy_gettext("New Review") + elif self == NotificationType.THREAD_REPLY: + # NOTE: THREAD_REPLY notification type + return lazy_gettext("Thread Reply") + elif self == NotificationType.BOT: + # NOTE: BOT notification type + return lazy_gettext("Bot") + elif self == NotificationType.MAINTAINER: + # NOTE: MAINTAINER notification type + return lazy_gettext("Maintainer") + elif self == NotificationType.EDITOR_ALERT: + # NOTE: EDITOR_ALERT notification type + return lazy_gettext("Editor Alert") + elif self == NotificationType.EDITOR_MISC: + # NOTE: EDITOR_MISC notification type + return lazy_gettext("Editor Misc") + elif self == NotificationType.OTHER: + # NOTE: OTHER notification type + return lazy_gettext("Other") + else: + raise "Unknown notification type" def to_name(self): return self.name.lower() - def get_description(self): + @property + def this_is(self): if self == NotificationType.PACKAGE_EDIT: - return "When another user edits your packages, releases, etc." + return lazy_gettext("This is a Package Edit notification.") elif self == NotificationType.PACKAGE_APPROVAL: - return "Notifications from editors related to the package approval process." + return lazy_gettext("This is a Package Approval notification.") elif self == NotificationType.NEW_THREAD: - return "When a thread is created on your package." + return lazy_gettext("This is a New Thread notification.") elif self == NotificationType.NEW_REVIEW: - return "When a user posts a review on your package." + return lazy_gettext("This is a New Review notification.") elif self == NotificationType.THREAD_REPLY: - return "When someone replies to a thread you're watching." + return lazy_gettext("This is a Thread Reply notification.") elif self == NotificationType.BOT: - return "From a bot - for example, update notifications." + return lazy_gettext("This is a Bot notification.") elif self == NotificationType.MAINTAINER: - return "When your package's maintainers change." + return lazy_gettext("This is a Maintainer change notification.") elif self == NotificationType.EDITOR_ALERT: - return "For editors: Important alerts." + return lazy_gettext("This is an Editor Alert notification.") elif self == NotificationType.EDITOR_MISC: - return "For editors: Minor notifications, including new threads." + return lazy_gettext("This is an Editor Misc notification.") elif self == NotificationType.OTHER: - return "Minor notifications not important enough for a dedicated category." + return lazy_gettext("This is an Other notification.") else: - return "" + raise "Unknown notification type" + + @property + def description(self): + if self == NotificationType.PACKAGE_EDIT: + return lazy_gettext("When another user edits your packages, releases, etc.") + elif self == NotificationType.PACKAGE_APPROVAL: + return lazy_gettext("Notifications from editors related to the package approval process.") + elif self == NotificationType.NEW_THREAD: + return lazy_gettext("When a thread is created on your package.") + elif self == NotificationType.NEW_REVIEW: + return lazy_gettext("When a user posts a review on your package.") + elif self == NotificationType.THREAD_REPLY: + return lazy_gettext("When someone replies to a thread you're watching.") + elif self == NotificationType.BOT: + return lazy_gettext("From a bot - for example, update notifications.") + elif self == NotificationType.MAINTAINER: + return lazy_gettext("When your package's maintainers change.") + elif self == NotificationType.EDITOR_ALERT: + return lazy_gettext("For editors: Important alerts.") + elif self == NotificationType.EDITOR_MISC: + return lazy_gettext("For editors: Minor notifications, including new threads.") + elif self == NotificationType.OTHER: + return lazy_gettext("Minor notifications not important enough for a dedicated category.") + else: + raise "Unknown notification type" def __str__(self): return self.name @@ -439,7 +496,7 @@ class NotificationType(enum.Enum): @classmethod def choices(cls): - return [(choice, choice.get_title()) for choice in cls] + return [(choice, choice.title) for choice in cls] @classmethod def coerce(cls, item): diff --git a/app/querybuilder.py b/app/querybuilder.py index 1b24bdce..7b6c33df 100644 --- a/app/querybuilder.py +++ b/app/querybuilder.py @@ -74,6 +74,10 @@ class QueryBuilder: return ret + @property + def query_hint(self): + return self.title + @property def noindex(self): return (self.search is not None or len(self.tags) > 1 or len(self.types) > 1 or len(self.hide_flags) > 0 or diff --git a/app/templates/base.html b/app/templates/base.html index 7a1bc0bf..2256f8b6 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -55,7 +55,7 @@ {% if type %}{% endif %}