Skip to content

Commit

Permalink
Rebuild translation coverage with ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyStk committed Aug 18, 2024
1 parent d9fc022 commit 29a5da4
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{% extends "../_todo_dashboard_row.html" %}
{% load i18n %}
{% block todo_dashboard_ajax_url %}
{{ translation_coverage_ajax }}
{% endblock todo_dashboard_ajax_url %}
{% block todo_dashboard_id %}
translation-coverage
{% endblock todo_dashboard_id %}
{% block todo_dashboard_icon %}
languages
{% endblock todo_dashboard_icon %}
Expand All @@ -15,24 +21,33 @@
{% endwith %}
{% endblock todo_dashboard_number %}
{% block todo_dashboard_description %}
{% if number_of_missing_or_outdated_translations > 0 %}
<div class="hidden todo-message">
{# djlint:off #}
{% blocktranslate trimmed %}
Your pages currently have <b></b> <b>pages</b> that have an outdated or no translation.
In order for the users to benefit from your content you should translate them or have them translated.
{% endblocktranslate %}
{# djlint:on #}
</div>
<div class="hidden success-message">
{% blocktranslate trimmed %}
Your pages currently have <b>{{ number_of_missing_or_outdated_translations }} pages </b>that have an outdated or no translation.
In order for the users to benefit from your content you should translate them or have them translated.
At the moment all pages have up-to-date translations. Good job!
{% endblocktranslate %}
{% else %}
</div>
<div class="waiting-message">
{% blocktranslate trimmed %}
At the moment all pages have up-to-date translations. Good job!
We are loading your outdated and missing translations in the background. Please be patient.
{% endblocktranslate %}
{% endif %}
</div>
{% endblock todo_dashboard_description %}
{% block todo_dashboard_button_link %}
{% if outdated_pages %}
<div class="hidden todo-button">
<a class="btn !rounded-full"
href="{% url 'translation_coverage' region_slug=request.region.slug %}">
{% translate "Go to <br> translation coverage" %}
</a>
{% else %}
</div>
<div class="hidden success-icon">
<i class="w-8 h-8 text-green-500" icon-name="check-circle-2"></i>
{% endif %}
</div>
{% endblock todo_dashboard_button_link %}
7 changes: 6 additions & 1 deletion integreat_cms/cms/urls/protected.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,12 @@
"broken-links/",
dashboard.DashboardView.get_broken_links_context,
name="get_broken_links_ajax",
)
),
path(
"translation-coverage/",
dashboard.DashboardView.get_translation_coverage_context,
name="get_translation_coverage_ajax",
),
]
),
),
Expand Down
33 changes: 20 additions & 13 deletions integreat_cms/cms/views/dashboard/dashboard_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
"get_broken_links_ajax",
kwargs={"region_slug": self.request.region.slug},
),
"translation_coverage_ajax": reverse(
"get_translation_coverage_ajax",
kwargs={"region_slug": self.request.region.slug},
),
}
)

Expand All @@ -74,7 +78,6 @@ def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
context.update(self.get_low_hix_value_context())
context.update(self.get_outdated_pages_context())
context.update(self.get_drafted_pages())
context.update(self.get_translation_coverage_context())

return context

Expand Down Expand Up @@ -261,32 +264,34 @@ def get_drafted_pages(
"single_drafted_page": single_drafted_page,
}

@json_response
# pylint: disable=unused-argument, disable=no-self-argument
def get_translation_coverage_context(
self,
) -> dict[str, QuerySet | PageTranslation | datetime | int | None]:
request: HttpRequest, region_slug: str
) -> JsonResponse:
r"""
Extend context by info on translation coverage of pages
:return: Dictionary containing the context for translation coverage of pages in a region
"""
number_of_outdated_pages = 0

if not self.request.region.default_language:
if not request.region.default_language:
return {}

languages = LanguageTreeNode.objects.filter(region=self.request.region).exclude(
language=self.request.region.default_language
languages = LanguageTreeNode.objects.filter(region=request.region).exclude(
language=request.region.default_language
)

pages_in_region = Page.objects.filter(region=self.request.region)
pages_in_region = Page.objects.filter(region=request.region)
possible_translations = languages.count() * pages_in_region.count()

published_foreign_translations = (
PageTranslation.objects.select_related("language")
.order_by("page__id", "language__id", "-version")
.distinct("page__id", "language__id")
.filter(page__region__slug=self.request.region.slug, minor_edit=False)
.exclude(language=self.request.region.default_language)
.filter(page__region__slug=request.region.slug, minor_edit=False)
.exclude(language=request.region.default_language)
.all()
)

Expand All @@ -298,7 +303,9 @@ def get_translation_coverage_context(
if pagetranslation.is_outdated:
number_of_outdated_pages += 1

return {
"number_of_missing_or_outdated_translations": number_of_missing_translations
+ number_of_outdated_pages
}
return JsonResponse(
data={
"number_of_missing_or_outdated_translations": number_of_missing_translations
+ number_of_outdated_pages
}
)
47 changes: 39 additions & 8 deletions integreat_cms/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -5281,13 +5281,15 @@ msgid ""
"Your pages currently have <b>%(number_of_missing_translations)s pages </"
"b>that have an outdated or no translation. In order for the users to benefit "
"from your content you should translate them or have them translated."
msgstr "Ihre Inhalte haben momentan <b>%(number_of_missing_translations)s Seiten</b> mit fehlender oder veralteter Übersetzung. Damit die Nutzer:innen von Ihren Inhalten profitieren können, sollten Sie diese übersetzen (lassen)."
msgstr ""
"Ihre Inhalte haben momentan <b>%(number_of_missing_translations)s Seiten</b> "
"mit fehlender oder veralteter Übersetzung. Damit die Nutzer:innen von Ihren "
"Inhalten profitieren können, sollten Sie diese übersetzen (lassen)."

#: cms/templates/dashboard/todo_dashboard_rows/_missing_translations_row.html
#: cms/templates/dashboard/todo_dashboard_rows/number_of_missing_or_outdated_translations_row.html
msgid "At the moment all pages have up-to-date translations. Good job!"
msgstr ""
"Aktuell haben alle Ihre Seiten aktuelle Übersetzungen. Gute Arbeit!"
msgstr "Aktuell haben alle Ihre Seiten aktuelle Übersetzungen. Gute Arbeit!"

#: cms/templates/dashboard/todo_dashboard_rows/_missing_translations_row.html
#: cms/templates/dashboard/todo_dashboard_rows/number_of_missing_or_outdated_translations_row.html
Expand Down Expand Up @@ -5355,12 +5357,22 @@ msgid "At the moment there is no page waiting for approval. Good job!"
msgstr "Aktuell liegen keine Seiten zur Freigabe vor. Gute Arbeit!"

#: cms/templates/dashboard/todo_dashboard_rows/number_of_missing_or_outdated_translations_row.html
#, python-format
msgid ""
"Your pages currently have <b>%(number_of_missing_or_outdated_translations)s "
"pages </b>that have an outdated or no translation. In order for the users to "
"benefit from your content you should translate them or have them translated."
msgstr "Ihre Inhalten haben aktuell <b>%(number_of_missing_or_outdated_translations)s Seiten </b> mit fehlender oder veralteter Übersetzung. Damit die Nutzer:innen von Ihren Inhalten profitieren können, sollten Sie diese übersetzen (lassen)."
"Your pages currently have <b></b> <b>pages</b> that have an outdated or no "
"translation. In order for the users to benefit from your content you should "
"translate them or have them translated."
msgstr ""
"Ihre Inhalte haben momentan <b></b> <b>Seiten</b> mit fehlender oder "
"veralteter Übersetzung. Damit die Nutzer:innen von Ihren Inhalten "
"profitieren können, sollten Sie diese übersetzen (lassen)."

#: cms/templates/dashboard/todo_dashboard_rows/number_of_missing_or_outdated_translations_row.html
msgid ""
"We are loading your outdated and missing translations in the background. "
"Please be patient."
msgstr ""
"Wir laden im Hintergrund gerade Ihre kaputten Links. Bitte haben Sie noch "
"ein wenig Geduld."

#: cms/templates/emails/_base_email.html
#: cms/templates/emails/password_reset_email.txt
Expand Down Expand Up @@ -10334,6 +10346,25 @@ msgstr ""
#~ msgid "Contents"
#~ msgstr "Inhalte"

#~ msgid ""
#~ "We are loading your translation coverage in the background. Please be "
#~ "patient."
#~ msgstr ""
#~ "Wir laden im Hintergrund gerade Ihre veralteten und fehlenden "
#~ "Übersetzungen. Bitte haben Sie noch ein wenig Geduld."

#, python-format
#~ msgid ""
#~ "Your pages currently have "
#~ "<b>%(number_of_missing_or_outdated_translations)s pages </b>that have an "
#~ "outdated or no translation. In order for the users to benefit from your "
#~ "content you should translate them or have them translated."
#~ msgstr ""
#~ "Ihre Inhalten haben aktuell "
#~ "<b>%(number_of_missing_or_outdated_translations)s Seiten </b> mit "
#~ "fehlender oder veralteter Übersetzung. Damit die Nutzer:innen von Ihren "
#~ "Inhalten profitieren können, sollten Sie diese übersetzen (lassen)."

#~ msgid "View location"
#~ msgstr "Ort ansehen"

Expand Down
1 change: 1 addition & 0 deletions integreat_cms/static/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import "./js/menu";

import "./js/poi-categories/poicategory-colors-icons";
import "./js/dashboard/broken-links";
import "./js/dashboard/translation-coverage";

// IE11: fetch
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
Expand Down
84 changes: 84 additions & 0 deletions integreat_cms/static/src/js/dashboard/translation-coverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { getCsrfToken } from "../utils/csrf-token";

type Content = {
number_of_missing_or_outdated_translations: number;
};

const getContent = async (url: string): Promise<Content> => {
const response = await fetch(url, {
method: "POST",
headers: {
"X-CSRFToken": getCsrfToken(),
},
});
return response.json();
};

const showAllTotalNumbers = () => {
const elements = document.querySelectorAll<HTMLElement>(".total-results");

elements.forEach((element) => {
if (!element.closest("#translation-coverage")) {
const el = element;
el.classList.remove("hidden");
}
});
};

window.addEventListener("load", async () => {
showAllTotalNumbers();

const translationCoverageElement = document.getElementById("translation-coverage");

if (!translationCoverageElement) {
return;
}

const url = translationCoverageElement.dataset.url;
const hideWaitingMessage = () => {
translationCoverageElement.querySelector(".waiting-message").classList.add("hidden");
};

const showSuccessMessage = () => {
translationCoverageElement.querySelector(".success-message").classList.remove("hidden");
};

const showSuccessIcon = () => {
translationCoverageElement.querySelector(".success-icon").classList.remove("hidden");
};

const showDescription = (affectedPageTitle: string) => {
translationCoverageElement.querySelector(".todo-message").classList.remove("hidden");
(translationCoverageElement.querySelector(".todo-message b") as HTMLElement).innerText = affectedPageTitle;
};

const showNumberOfPagesElement = () => {
translationCoverageElement.querySelector(".total-results").classList.remove("hidden");
};

const updateNumberOfPages = (numberOfPages: number) => {
(translationCoverageElement.querySelector(".total-results span") as HTMLElement).innerText =
numberOfPages.toString();
};

const showButton = () => {
translationCoverageElement.querySelector(".todo-button").classList.remove("hidden");
};

if (url) {
console.log(url);
const json = await getContent(url);
console.log(json);
hideWaitingMessage();
showNumberOfPagesElement();
if (json.number_of_missing_or_outdated_translations > 0) {
showDescription(String(json.number_of_missing_or_outdated_translations));
updateNumberOfPages(json.number_of_missing_or_outdated_translations);
showButton();
} else {
showSuccessMessage();
showSuccessIcon();
updateNumberOfPages(0);
}
}
});

0 comments on commit 29a5da4

Please sign in to comment.