Translations including variables

The strings marked for translation can include placeholders to include variables in the translations. The following code is an example of a translation string with a placeholder:

from django.utils.translation import gettext as _month = _('April')day = '14'output = _('Today is %(month)s %(day)s') % {'month': month,                                            'day': day}

By using placeholders, you can reorder the text variables. For example, an English translation of the previous example might be "Today is April 14", while the Spanish one is "Hoy es 14 de Abril". Always use string interpolation instead of positional interpolation when you have more than one parameter for the translation string. By doing so, you will be able to reorder the placeholder ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.