Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Runtime decision on language support

18 views
Skip to first unread message

Shawn Thomas

unread,
Apr 24, 2023, 5:19:43 PM4/24/23
to Django users
Hi,

I've searched online for an answer to this but couldn't find anything. I have a multi-tenant Django app that supports multiple languages, both in code (django.po/django.mo) and for customer data. However, not every customer wants to translate their data into every language we support (think customers in different countries where some languages are less relevant as in others).

What I'm hoping to do is offer per-tenant config for which subset of our supported languages the app will offer.  I haven't yet seen how I can override LocaleMiddleware to suppress translation to a language a customer has chosen to not offer (but our django.po/django.mo supports it).

The end goal is to prevent an inconsistent experience where app UI is returned in a supported language but all the customer data shown is in English, for example.

Thank you for any pointers!

Best regards,
Shawn



ASAMOAH EMMANUEL

unread,
Apr 25, 2023, 1:22:34 AM4/25/23
to django...@googlegroups.com

To achieve this, you can create a custom middleware that inherits from `LocaleMiddleware`. In your custom middleware, you can override the `process_request` method to check for the tenant's allowed languages and set the request's language accordingly.

1. Create a new model field for the Tenant model that stores the allowed languages as a list. This can be a JSON field or a ManyToMany field linked to a Language model.

# models.py
from django.contrib.postgres.fields import ArrayField
from django.db import models

class Tenant(models.Model):
    ...
    allowed_languages = ArrayField(models.CharField(max_length=10))


2. Create a custom middleware that inherits from `django.middleware.locale.LocaleMiddleware`:
# custom_middleware.py
from django.middleware.locale import LocaleMiddleware
from django.utils import translation

class CustomLocaleMiddleware(LocaleMiddleware):
    def process_request(self, request):
        # Get the current tenant
        current_tenant = request.tenant

        # Get the user's preferred language from the request
        language = translation.get_language_from_request(request, check_path=True)

        # Check if the preferred language is in the tenant's allowed_languages list
        if language not in current_tenant.allowed_languages:
            # If not, set the default language for the tenant
            language = current_tenant.default_language

        # Activate the chosen language
        translation.activate(language)
        request.LANGUAGE_CODE = translation.get_language()
3. Replace `LocaleMiddleware` with your custom middleware in your `MIDDLEWARE` settings:

# settings.py
MIDDLEWARE = [
    ...
    # 'django.middleware.locale.LocaleMiddleware',
    'your_app.custom_middleware.CustomLocaleMiddleware'.
Now, when a user requests a page, the custom middleware checks if the requested language is in the tenant's `allowed_languages` list. If it is not, the tenant's default language will be used. This ensures that the UI and the data are shown in a consistent language.

Shawn Thomas

unread,
Apr 25, 2023, 12:56:34 PM4/25/23
to django...@googlegroups.com

Thank you so much for your incredibly thorough response, Emmanuel! That is exactly the kind of insight I was hoping for.

 

Best,
Shawn

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/dlE8lWEPMDg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABFHQYzdc29n%2BBA3DfVo7HSh3%2B1XPQCSM%3DsXrwHPTVzVw%2B8-sA%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages