Home > Blockchain >  How do I translate the radio selection buttons?
How do I translate the radio selection buttons?

Time:03-23

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 >
  <label >{% trans "Country" %}:</label>
  <div >
     {{ 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'}),

   )

CodePudding user response:

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.

  • Related