|
4 | 4 |
|
5 | 5 | from django.db import transaction
|
6 | 6 | from django.db.models.signals import post_save
|
| 7 | +from django.utils.translation import get_language |
7 | 8 |
|
8 | 9 | from caluma.caluma_core.events import on
|
9 | 10 | from caluma.caluma_form import models as caluma_form_models
|
@@ -102,11 +103,26 @@ def update_table_summary_from_table_question(instance, *args, **kwargs):
|
102 | 103 | update_table_summary(instance=ad)
|
103 | 104 |
|
104 | 105 |
|
| 106 | +def get_translation_with_fallback(value): |
| 107 | + # This should not be necessary, because LOCALIZED_FIELDS_FALLBACKS is set |
| 108 | + # https://django-localized-fields.readthedocs.io/en/latest/settings.html#localized-fields-fallbacks |
| 109 | + lang = get_language() |
| 110 | + fallback_langs = ["en", "de"] |
| 111 | + fallback_langs.remove(lang) |
| 112 | + translated = getattr(value, lang) |
| 113 | + if not translated: |
| 114 | + for fl in fallback_langs: |
| 115 | + translated = getattr(value, fl) |
| 116 | + if translated: |
| 117 | + break |
| 118 | + return translated |
| 119 | + |
| 120 | + |
105 | 121 | def _make_csv_summary(table_answer):
|
106 | 122 | def get_answer_value(answer):
|
107 | 123 | value = answer.value or answer.date
|
108 | 124 | if options := answer.selected_options:
|
109 |
| - value = ",".join([str(o.label) for o in options]) |
| 125 | + value = ",".join([get_translation_with_fallback(o.label) for o in options]) |
110 | 126 | return value
|
111 | 127 |
|
112 | 128 | def get_lines(ads, q_slugs_and_labels):
|
@@ -146,4 +162,7 @@ def get_lines(ads, q_slugs_and_labels):
|
146 | 162 |
|
147 | 163 | def _sorted_form_question_slugs_and_labels(form):
|
148 | 164 | fqs = caluma_form_models.FormQuestion.objects.filter(form=form).order_by("-sort")
|
149 |
| - return [(fq.question.slug, str(fq.question.label)) for fq in fqs] |
| 165 | + return [ |
| 166 | + (fq.question.slug, get_translation_with_fallback(fq.question.label)) |
| 167 | + for fq in fqs |
| 168 | + ] |
0 commit comments