[Answered ]-Django translations of path **kwargs in urls.py?

1πŸ‘

βœ…

The main problem is that pgettext is eagerly: it translates text when it is given that. So here when it interprets urls.py (for the first time), so before the moment the server starts accepting requests.

We need to postpone the translation process until we need the string, and thus then look for the language context. This is why pgettext_lazy is used:

from django.urls import path
from django.utils.translation import pgettext_lazy
from main.views import SharedIndexView

app_name = 'some_site'

urlpatterns = [
    path(
        '',
        SharedIndexView.as_view(),
        {'SITE_SEO_title': pgettext_lazy('home', 'SITE_SEO_title')},
    )
]

This will thus return an object that, when needed, translate the string.

Leave a comment