0

django translation does not happen for a field using choices..

models.py:

 class Reservation(models.Model):
  class Period:
    MORNING = 'morning'
    EVENING = 'evening'

    @classmethod
    def choices(cls):
        return (                
            (cls.MORNING, _('Morning')),
            (cls.EVENING, '{} until {:%I %p}'.format(_('Evening'), datetime.time(16)),
        )       

 period = models.CharField(max_length=10, choices=Period.choices(),)

I am using {{ reservation.get_period_display }} in the template to display the field in few languages.Translation is fine for first choice cls.MORNING: الصباح. For the second choice (cls.EVENING), translation does not happen:"Evening until 04 PM"

Can anyone suggest me what is wrong with the format specifier, and why translation/localization is not working in this case.

N.B: The translation and i18n is setup properly and other places in my project is correctly showing translated values.

1 Answers1

2

You should wrap second line string in ugettex_lazy not just Evening

(cls.EVENING, _('{} until {:%I %p}').format(datetime.time(16)),

Read more Strings won't be translated in Django using format function available in Python 2.7

Also be sure to add #, python-brace-format. Like in this example

#: accounts/forms.py:691
#, python-brace-format
msgid "La validation de votre carte a échoué: ({0}) {1}"
msgstr ""
Community
  • 1
  • 1
Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63