Use translation on package view page

This commit is contained in:
rubenwardy 2024-02-25 18:24:30 +00:00
parent ab57b6aa2c
commit d252d687fc
2 changed files with 36 additions and 12 deletions

@ -19,8 +19,9 @@ import datetime
import enum import enum
import os import os
import typing
from flask import url_for from flask import url_for
from flask_babel import lazy_gettext from flask_babel import lazy_gettext, get_locale
from flask_sqlalchemy import BaseQuery from flask_sqlalchemy import BaseQuery
from sqlalchemy import or_, func from sqlalchemy import or_, func
from sqlalchemy.dialects.postgresql import insert from sqlalchemy.dialects.postgresql import insert
@ -493,6 +494,28 @@ class Package(db.Model):
def get_id(self): def get_id(self):
return "{}/{}".format(self.author.username, self.name) return "{}/{}".format(self.author.username, self.name)
def get_translated(self, lang=None):
if lang is None:
locale = get_locale()
if locale:
lang = locale.language
else:
lang = "en"
translation: typing.Optional[PackageTranslation] = self.translations.filter_by(language_id=lang).first()
if translation is None:
return {
"title": self.title,
"short_desc": self.short_desc,
"desc": self.desc,
}
return {
"title": translation.title or self.title,
"short_desc": translation.short_desc or self.short_desc,
"desc": translation.desc or self.desc,
}
def get_sorted_dependencies(self, is_hard=None): def get_sorted_dependencies(self, is_hard=None):
query = self.dependencies query = self.dependencies
if is_hard is not None: if is_hard is not None:

@ -1,14 +1,15 @@
{% set query=package.name %}
{% set release = package.get_download_release() %}
{% extends "base.html" %} {% extends "base.html" %}
{% set query=package.name %}
{% set release = package.get_download_release() %}
{% set meta = package.get_translated() %}
{% block title %} {% block title %}
{{ package.title }} {{ meta.title }}
{% endblock %} {% endblock %}
{% block description -%} {% block description -%}
{{ package.short_desc }} {{ meta.short_desc }}
{% endblock %} {% endblock %}
{% block headextra %} {% block headextra %}
@ -130,11 +131,11 @@
</div> </div>
<h1 class="display-3"> <h1 class="display-3">
{{ package.title }} {{ meta.title }}
</h1> </h1>
<p class="lead"> <p class="lead">
{{ package.short_desc }} {{ meta.short_desc }}
</p> </p>
<p> <p>
@ -322,9 +323,9 @@
</ul> </ul>
{% endif %} {% endif %}
{% if package.desc %} {% if meta.desc %}
<article class="markdown panel mb-5"> <article class="markdown panel mb-5">
{{ package.desc | markdown }} {{ meta.desc | markdown }}
</article> </article>
{% endif %} {% endif %}
@ -454,7 +455,7 @@
<a class="badge bg-primary" <a class="badge bg-primary"
href="{{ dep.package.get_url('packages.view') }}"> href="{{ dep.package.get_url('packages.view') }}">
{{ _("%(title)s by %(display_name)s", {{ _("%(title)s by %(display_name)s",
title=dep.package.title, display_name=dep.package.author.display_name) }} title=dep.package.get_translated().title, display_name=dep.package.author.display_name) }}
</a> </a>
{% elif dep.meta_package %} {% elif dep.meta_package %}
<a class="badge bg-primary" <a class="badge bg-primary"
@ -478,7 +479,7 @@
<a class="badge bg-secondary" <a class="badge bg-secondary"
href="{{ dep.package.get_url('packages.view') }}"> href="{{ dep.package.get_url('packages.view') }}">
{{ _("%(title)s by %(display_name)s", {{ _("%(title)s by %(display_name)s",
title=dep.package.title, display_name=dep.package.author.display_name) }} title=dep.package.get_translated().title, display_name=dep.package.author.display_name) }}
{% elif dep.meta_package %} {% elif dep.meta_package %}
<a class="badge bg-secondary" <a class="badge bg-secondary"
href="{{ url_for('modnames.view', name=dep.meta_package.name) }}"> href="{{ url_for('modnames.view', name=dep.meta_package.name) }}">