How to configure dynamic language switching for API responses using i18n_patterns in Django?

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:

  1. In settings.py, I’ve defined the available languages:

    LANGUAGES = [
        ('en', 'English'),
        ('ml', 'Malayalam'),
    ]
    LANGUAGE_CODE = 'en'  
    
  2. I’ve added 'django.middleware.locale.LocaleMiddleware' to the MIDDLEWARE setting:

    MIDDLEWARE = [
        ...  # other middlewares
        'django.middleware.locale.LocaleMiddleware',
    ]
    
    
  3. 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)
    
    

    The Issue:

    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)

    What I Expect:

    • I expect the API responses to be auto-translated into the selected language (e.g., Malayalam) based on the URL prefix or Accept-Language header.

    What I Have Tried:

    • 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/)

    My Questions:

    1. Is there any additional configuration required to automatically translate API responses based on the selected language?

    2. 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!

1. Is there any additional configuration required to automatically translate API responses based on the selected language?

Yes, there are a few additional steps you need to take to ensure your API responses are automatically translated:

  • Mark strings for translation: Use gettext or gettext_lazy in your views, serializers, and models to mark strings that need translation.
  • Generate and compile translation files: Use django-admin makemessages and django-admin compilemessages to create and compile .po and .mo files for each language.
  • Ensure LocaleMiddleware is correctly configured: It should be placed after SessionMiddleware and CommonMiddleware in your MIDDLEWARE settings.
  • Set LOCALE_PATHS: Ensure Django knows where to find your translation files by setting LOCALE_PATHS in settings.py.

2. 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?

To dynamically translate API responses based on the URL prefix or Accept-Language header:

  1. 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)
    
  2. 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
        ...
    ]
    
  3. 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})
    
  4. Generate and compile translation files:

    • Run django-admin makemessages -l ml to generate .po files for Malayalam.
    • Edit the .po files to add translations.
    • Run django-admin compilemessages to compile .po files into .mo files.

That's it I think!

Back to Top