I am working on a Django project and need to implement multilingual support for my REST API. I want the API to return responses in the selected language based on the URL prefix (e.g., /en/
, /ml/
) using i18n_patterns
.
I’ve done the following:
In settings.py
, I’ve defined the available languages:
LANGUAGES = [
('en', 'English'),
('ml', 'Malayalam'),
]
LANGUAGE_CODE = 'en'
I’ve added 'django.middleware.locale.LocaleMiddleware'
to the MIDDLEWARE
setting:
MIDDLEWARE = [
... # other middlewares
'django.middleware.locale.LocaleMiddleware',
]
In the project’s urls.py
, I’ve used i18n_patterns
to ensure the URLs are prefixed with the language code:
from django.conf.urls.i18n import i18n_patterns
urlpatterns = [
path('admin/', admin.site.urls),
path('auth/', include('accounts.urls')),
path('app/', include('app.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns = i18n_patterns(*urlpatterns)
While I can specify the language via the URL (e.g., /ml/
for Malayalam), the API responses are not being automatically translated to the selected language. The responses remain in English even when the Accept-Language
header or language prefix is set to another language (e.g., Malayalam)
Accept-Language
header.I’ve confirmed that LocaleMiddleware
is active in MIDDLEWARE
.
I’ve ensured that the correct language prefix is included in the URL (e.g., /ml/
)
Is there any additional configuration required to automatically translate API responses based on the selected language?
How can I ensure that the API responses are dynamically translated into the correct language (e.g., Malayalam) based on the URL prefix or Accept-Language
headers?
Any help or code examples would be greatly appreciated!
Yes, there are a few additional steps you need to take to ensure your API responses are automatically translated:
gettext
or gettext_lazy
in your views, serializers, and models to mark strings that need translation.django-admin makemessages
and django-admin compilemessages
to create and compile .po
and .mo
files for each language.LocaleMiddleware
is correctly configured: It should be placed after SessionMiddleware
and CommonMiddleware
in your MIDDLEWARE
settings.LOCALE_PATHS
: Ensure Django knows where to find your translation files by setting LOCALE_PATHS
in settings.py
.To dynamically translate API responses based on the URL prefix or Accept-Language
header:
Use i18n_patterns
in urls.py
:
Wrap your URL patterns with i18n_patterns
to enable language prefixes (e.g., /ml/
, /en/
).
from django.conf.urls.i18n import i18n_patterns
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('auth/', include('accounts.urls')),
path('app/', include('app.urls'))
]
urlpatterns = i18n_patterns(*urlpatterns)
Ensure LocaleMiddleware
is active:
The LocaleMiddleware
automatically activates the language based on the URL prefix or Accept-Language
header. Ensure it’s in your MIDDLEWARE
:
MIDDLEWARE = [
...
'django.middleware.locale.LocaleMiddleware', # After SessionMiddleware and CommonMiddleware
...
]
Mark strings for translation in your views:
Use gettext
or gettext_lazy
to mark strings that need translation. For example:
from django.utils.translation import gettext as _
def my_view(request):
output = _("Hello, world!")
return Response({"message": output})
Generate and compile translation files:
django-admin makemessages -l ml
to generate .po
files for Malayalam..po
files to add translations.django-admin compilemessages
to compile .po
files into .mo
files.That's it I think!