Dor
Dor

Reputation: 902

Excluding a Django app from being localized using a middleware

I need to localize a django project, but keep one of the applications (the blog) English only.

I wrote this middleware in order to achieve this:

from django.conf import settings
from django.core.urlresolvers import resolve

class DelocalizeMiddleware:
    def process_request(self, request):
        current_app_name = __name__.split('.')[-2]
        match = resolve(request.path)
        if match.app_name == current_app_name:
            request.LANGUAGE_CODE = settings.LANGUAGE_CODE

Problem is, it assumes the middleware lies directly in the application module (e.g. blog/middleware.py) for retrieving the app name. Other projects might have the middleware in blog/middleware/delocalize.py or something else altogether.

What's the best way to retrieve the name of the currently running app?

Upvotes: 2

Views: 403

Answers (1)

Matthew J Morrison
Matthew J Morrison

Reputation: 4403

You can use Django's resolve function to get the current app name.

https://docs.djangoproject.com/en/dev/topics/http/urls/#resolve

Upvotes: 1

Related Questions