How to Iterate over the translations in HTML templates

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.

I’m not sure I get exactly what you are trying to do, but as I understand it, I would replace the first 6 lines of your DocList function by this:

    types = list(Pdffile.objects.all().values_list('pdf_type', flat=True).distinct())
    types_dict = dict(Pdffile.TYPE_CHOICES)
    translated_pdf_types = [types_dict[val] for val in types]

Hope this helps.

Hi @claudep,

This is exactly what I’ve been looking for as a solution, with the code that you provided when a user visits the rendered library_list.html and changes the language then in the below code I get the right corresponding translations:

	{% for translated_pdf_type in translated_pdf_types %}
		<li><a href="#" data-filter=".{{ translated_pdf_type }}">{{ translated_pdf_type }}</a></li>
	{% endfor %}

I was wondering that How can I get the first {{ translated_pdf_type }} in the li tag in english (the default language) in the rendered library_list.html and the 2nd one is fine which is translated now.
I am using django-modeltranslation and django-rosetta.
or, is there any way so that I can get the {{ pdf.pdf_type }} in a loop based on the visitor’s selected language in the rendered library_list.html page, in the below code I can get the variable in english but what if I need their translations depending on the selected language by the page visitor? like:

{% for pdf in pdffiles %}
    {{ pdf.pdf_type }}
{% endfor %}

Thank you.