Hello,
I have a model called Pdffile, in the model I have a choice field which needs to get translations in ckb and ar by using the rosetta I did the translations and in django admin panel when I change the language I get the equivalent translations but I do not know how to get those traslated valued in a loop in the rendered html templates:
I am using django 4.2.14
models.py
from django.utils.translation import gettext_lazy as _
class Pdffile(models.Model):
filename = models.CharField(max_length=100)
TYPE_CHOICES = (
('manual', _('Manual')),
('catalogue', _('Catalogue')),
('application_details', _('Application Details')),
)
pdf_type = models.CharField(max_length=20, choices=TYPE_CHOICES)
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
Here I needed each value not to be repeated so I did as below
views.py
from django.shortcuts import render
from .models import Pdffile
from django.utils.translation import gettext
def DocList(request):
types = list(Pdffile.objects.all().values_list('pdf_type', flat=True))
final_list = []
for c in types:
if c not in final_list:
final_list.append(c)
translated_pdf_types = [gettext("pdf_type:%s" % pdf_type) for pdf_type in final_list]
context = {'translated_pdf_types': translated_pdf_types, }
return render(request, 'library/pdffile/library_list.html', context)
Here in the html I need to loop over translated_pdf_types and get the translated strings to be shown in the user selected languag
library_list.html
{% for translated_pdf_type in translated_pdf_types %}
<li><a href="#" data-filter=".{{ translated_pdf_type }}">{{ translated_pdf_type }}</a></li>
{% endfor %}
How can i achieve that is my question.
Thanks.