Thank you so much for your incredibly thorough response, Emmanuel! That is exactly the kind of insight I was hoping for.
Best, Shawn From: [email protected] <[email protected]> on behalf of ASAMOAH EMMANUEL <[email protected]> Date: Monday, April 24, 2023 at 10:22 PM To: [email protected] <[email protected]> Subject: Re: Runtime decision on language support 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. -- 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 [email protected]<mailto:[email protected]>. 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<https://groups.google.com/d/msgid/django-users/CABFHQYzdc29n%2BBA3DfVo7HSh3%2B1XPQCSM%3DsXrwHPTVzVw%2B8-sA%40mail.gmail.com?utm_medium=email&utm_source=footer>. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/DM4P223MB05429BEE155C61ECCF542FD9AF649%40DM4P223MB0542.NAMP223.PROD.OUTLOOK.COM.

