About Lazy String Concatenation

When you use lazy translations for the values of attributes of classes, and want to combine a string of multiple translatable strings, you have to use format_lazy() function, instead of any other string concatenation method:

from django import forms
from django.utils.text import format_lazy
from django.utils.translation import ugettext_lazy as _

class MyForm(forms.Form):
    q = forms.CharField(
        label=format_lazy(
            "{}: {}",
            _("Global search"),
            _("Enter a few characters for autosuggestion"),
        ),
        required=False,
    )

Tips and Tricks Programming Django 5.2 Django 4.2 Django 3.2 Django 2.2

Django/Python Consulting

If you have a specific Django challenge or integration you'd like to solve, I'd be happy to help. Book a free 30-minute call to discuss your project, see if we're a good fit, and explore the best approach for your needs. After the call, you'll receive a tailored cost estimate based on what we discuss.