diff --git a/caluma/extensions/events/form.py b/caluma/extensions/events/form.py index 176d633f..ed2f0c41 100644 --- a/caluma/extensions/events/form.py +++ b/caluma/extensions/events/form.py @@ -4,6 +4,7 @@ from django.db import transaction from django.db.models.signals import post_save +from django.utils.translation import get_language from caluma.caluma_core.events import on from caluma.caluma_form import models as caluma_form_models @@ -102,11 +103,26 @@ def update_table_summary_from_table_question(instance, *args, **kwargs): update_table_summary(instance=ad) +def get_translation_with_fallback(value): + # This should not be necessary, because LOCALIZED_FIELDS_FALLBACKS is set + # https://django-localized-fields.readthedocs.io/en/latest/settings.html#localized-fields-fallbacks + lang = get_language() + fallback_langs = ["en", "de"] + fallback_langs.remove(lang) + translated = getattr(value, lang) + if not translated: + for fl in fallback_langs: + translated = getattr(value, fl) + if translated: + break + return translated + + def _make_csv_summary(table_answer): def get_answer_value(answer): value = answer.value or answer.date if options := answer.selected_options: - value = ",".join([str(o.label) for o in options]) + value = ",".join([get_translation_with_fallback(o.label) for o in options]) return value def get_lines(ads, q_slugs_and_labels): @@ -146,4 +162,7 @@ def get_lines(ads, q_slugs_and_labels): def _sorted_form_question_slugs_and_labels(form): fqs = caluma_form_models.FormQuestion.objects.filter(form=form).order_by("-sort") - return [(fq.question.slug, str(fq.question.label)) for fq in fqs] + return [ + (fq.question.slug, get_translation_with_fallback(fq.question.label)) + for fq in fqs + ] diff --git a/caluma/extensions/tests/test_events.py b/caluma/extensions/tests/test_events.py index a36d280b..6dd75162 100644 --- a/caluma/extensions/tests/test_events.py +++ b/caluma/extensions/tests/test_events.py @@ -617,13 +617,15 @@ def add_table_with_summary( ) form_question_factory(form=row_form, question=row_question_2) + # Only set English label to test fallback row_question_3 = question_factory( type=Question.TYPE_CHOICE, slug="row3", - label="row3_label", + label={"en": "row3_label"}, is_required="true", is_hidden="false", ) + question_option_factory( question=row_question_3, option=option_factory(slug="o1", label="option1 label") )