Issue
I am using the i18n language change, the issue is that in several of my forms I was able to do the translation, the problem is that when there is a radio button in forms.py I don't know how to translate the options
html
<div class="mb-3">
<label class="form-label d-block mb-3">{% trans "Country" %}:</label>
<div class="custom-radio form-check form-check-inline">
{{ form.pais }}
</div>
</div>
forms.py
PAIS = (
('United States', 'United States'),
('Canada', 'Canada'),
('Other', 'Other'),
)
class ClientesForm(forms.ModelForm):
pais = forms.ChoiceField(
choices=PAIS,
widget=forms.RadioSelect(attrs={'class':'custom-radio-list'}),
)
Solution
You can work with the gettext_lazy(…) function [Django-doc] to work with lazy translatable strings:
from django.utils.translation import gettext_lazy as _
PAIS = (
('United States', _('United States')),
('Canada', _('Canada')),
('Other', _('Other')),
)
This will add translations for United States, Canada, etc. when you make translations, and translate the text when the form is rendered.
For more information, see the Lazy translations section of the documentation.
Answered By - Willem Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.