Translate question options in backend and PDFs (Z#23134850) (#3693)

* Translate question options in backend and PDFs

* Extend to invoices
This commit is contained in:
Raphael Michel
2023-11-13 15:48:45 +01:00
committed by GitHub
parent ee70fec7ad
commit 3c46c461c0
4 changed files with 22 additions and 5 deletions

View File

@@ -1271,6 +1271,21 @@ class QuestionAnswer(models.Model):
return self.file.name.split('.', 1)[-1]
def __str__(self):
return self.to_string(use_cached=True)
def to_string_i18n(self):
return self.to_string(use_cached=False)
def to_string(self, use_cached=True):
"""
Render this answer as a string.
:param use_cached: If ``True`` (default), choice and multiple choice questions will show their cached
value, i.e. the value of the selected options at the time of saving and in the language
the answer was saved in. If ``False``, the values will instead be loaded from the
database, yielding current and translated values of the options. However, additional database
queries might be required.
"""
if self.question.type == Question.TYPE_BOOLEAN and self.answer == "True":
return str(_("Yes"))
elif self.question.type == Question.TYPE_BOOLEAN and self.answer == "False":
@@ -1305,6 +1320,8 @@ class QuestionAnswer(models.Model):
return PhoneNumber.from_string(self.answer).as_international
except NumberParseException:
return self.answer
elif self.question.type in (Question.TYPE_CHOICE, Question.TYPE_CHOICE_MULTIPLE) and self.answer and not use_cached:
return ", ".join(str(o.answer) for o in self.options.all())
else:
return self.answer