From d2c57793018520aa308825f2d6e0ae68b94db56c Mon Sep 17 00:00:00 2001
From: rubenwardy
Date: Fri, 7 Jun 2024 05:28:57 +0100
Subject: [PATCH] Add ability to translate tags and content warnings
---
app/blueprints/packages/packages.py | 13 +++++-----
app/models/packages.py | 14 +++++++++++
app/querybuilder.py | 4 ++--
app/templates/admin/tags/edit.html | 2 +-
app/templates/index.html | 4 ++--
app/templates/packages/list.html | 8 +++----
app/templates/packages/view.html | 8 +++----
app/templates/todo/tags.html | 2 +-
app/utils/minetest_hypertext.py | 2 +-
utils/extract_translations.py | 37 +++++++++++++++++++++++++++++
10 files changed, 73 insertions(+), 21 deletions(-)
create mode 100755 utils/extract_translations.py
diff --git a/app/blueprints/packages/packages.py b/app/blueprints/packages/packages.py
index e5de11d1..9f35c4f9 100644
--- a/app/blueprints/packages/packages.py
+++ b/app/blueprints/packages/packages.py
@@ -217,11 +217,12 @@ def download(package):
return redirect(release.get_download_url())
-def makeLabel(obj):
- if obj.description:
- return "{}: {}".format(obj.title, obj.description)
+def make_label(obj: Tag | ContentWarning):
+ translated = obj.get_translated()
+ if translated["description"]:
+ return "{}: {}".format(translated["title"], translated["description"])
else:
- return obj.title
+ return translated["title"]
class PackageForm(FlaskForm):
@@ -232,8 +233,8 @@ class PackageForm(FlaskForm):
dev_state = SelectField(lazy_gettext("Maintenance State"), [InputRequired()], choices=PackageDevState.choices(with_none=True), coerce=PackageDevState.coerce)
- tags = QuerySelectMultipleField(lazy_gettext('Tags'), query_factory=lambda: Tag.query.order_by(db.asc(Tag.name)), get_pk=lambda a: a.id, get_label=makeLabel)
- content_warnings = QuerySelectMultipleField(lazy_gettext('Content Warnings'), query_factory=lambda: ContentWarning.query.order_by(db.asc(ContentWarning.name)), get_pk=lambda a: a.id, get_label=makeLabel)
+ tags = QuerySelectMultipleField(lazy_gettext('Tags'), query_factory=lambda: Tag.query.order_by(db.asc(Tag.name)), get_pk=lambda a: a.id, get_label=make_label)
+ content_warnings = QuerySelectMultipleField(lazy_gettext('Content Warnings'), query_factory=lambda: ContentWarning.query.order_by(db.asc(ContentWarning.name)), get_pk=lambda a: a.id, get_label=make_label)
license = QuerySelectField(lazy_gettext("License"), [DataRequired()], allow_blank=True, query_factory=lambda: License.query.order_by(db.asc(License.name)), get_pk=lambda a: a.id, get_label=lambda a: a.name)
media_license = QuerySelectField(lazy_gettext("Media License"), [DataRequired()], allow_blank=True, query_factory=lambda: License.query.order_by(db.asc(License.name)), get_pk=lambda a: a.id, get_label=lambda a: a.name)
diff --git a/app/models/packages.py b/app/models/packages.py
index 70b2ad72..60086cdf 100644
--- a/app/models/packages.py
+++ b/app/models/packages.py
@@ -906,6 +906,13 @@ class ContentWarning(db.Model):
regex = re.compile("[^a-z_]")
self.name = regex.sub("", self.title.lower().replace(" ", "_"))
+ def get_translated(self):
+ # Translations are automated on dynamic data using `extract_translations.py`
+ return {
+ "title": gettext(self.title),
+ "description": gettext(self.description),
+ }
+
def as_dict(self):
description = self.description if self.description != "" else None
return { "name": self.name, "title": self.title, "description": description }
@@ -931,6 +938,13 @@ class Tag(db.Model):
regex = re.compile("[^a-z_]")
self.name = regex.sub("", self.title.lower().replace(" ", "_"))
+ def get_translated(self):
+ # Translations are automated on dynamic data using `extract_translations.py`
+ return {
+ "title": gettext(self.title),
+ "description": gettext(self.description),
+ }
+
def as_dict(self):
description = self.description if self.description != "" else None
return {
diff --git a/app/querybuilder.py b/app/querybuilder.py
index cfd87de8..1b24bdce 100644
--- a/app/querybuilder.py
+++ b/app/querybuilder.py
@@ -60,9 +60,9 @@ class QueryBuilder:
if len(self.tags) == 0:
ret = package_type
elif len(self.tags) == 1:
- ret = self.tags[0].title + " " + package_type
+ ret = self.tags[0].get_translated()["title"] + " " + package_type
else:
- tags = ", ".join([tag.title for tag in self.tags])
+ tags = ", ".join([tag.get_translated()["title"] for tag in self.tags])
ret = f"{tags} - {package_type}"
if self.search:
diff --git a/app/templates/admin/tags/edit.html b/app/templates/admin/tags/edit.html
index bf559220..a7ba2595 100644
--- a/app/templates/admin/tags/edit.html
+++ b/app/templates/admin/tags/edit.html
@@ -2,7 +2,7 @@
{% block title %}
{% if tag %}
- Edit {{ tag.title }}
+ Edit {{ tag.get_translated().title }}
{% else %}
New tag
{% endif %}
diff --git a/app/templates/index.html b/app/templates/index.html
index e192c5eb..c6b7847f 100644
--- a/app/templates/index.html
+++ b/app/templates/index.html
@@ -166,9 +166,9 @@
{% set tag = pair[1] %}
- {{ tag.title }}
+ {{ tag.get_translated().title }}
{{ count }}
{% endfor %}
diff --git a/app/templates/packages/list.html b/app/templates/packages/list.html
index a49cbbf4..05b4c82d 100644
--- a/app/templates/packages/list.html
+++ b/app/templates/packages/list.html
@@ -31,16 +31,16 @@
{% if tag in selected_tags %}
- {{ tag.title }}
+ {{ tag.get_translated().title }}
{{ count }}
{% else %}
- {{ tag.title }}
+ {{ tag.get_translated().title }}
{{ count }}
{% endif %}
diff --git a/app/templates/packages/view.html b/app/templates/packages/view.html
index 63a5897a..3fb4ad65 100644
--- a/app/templates/packages/view.html
+++ b/app/templates/packages/view.html
@@ -163,11 +163,11 @@
{{ _("Work in Progress") }}
{% endif %}
- {% for t in package.tags %}
+ {% for tag in package.tags %}
- {{ t.title }}
+ title="{{ tag.get_translated().description or '' }}"
+ href="{{ url_for('packages.list_all', tag=tag.name) }}">
+ {{ tag.get_translated().title }}
{% endfor %}
diff --git a/app/templates/todo/tags.html b/app/templates/todo/tags.html
index b397517f..31576c3e 100644
--- a/app/templates/todo/tags.html
+++ b/app/templates/todo/tags.html
@@ -57,7 +57,7 @@
{% for tag in package.tags %}
- {{ tag.title }}
+ {{ tag.get_translated().title }}
{% endfor %}