from django.middleware.cache import CacheMiddleware

class VaryOnLangCacheMiddleware(CacheMiddleware):

    def __init__(self, **kwargs):
        CacheMiddleware.__init__(self, **kwargs)
        self.ori_key_prefix = self.key_prefix

    def process_request(self, request):
        # Reset key_prefix depending on language
        lang_suffix = '_%s' % request.LANGUAGE_CODE
        if not self.key_prefix.endswith(lang_suffix):
            self.key_prefix = self.ori_key_prefix + lang_suffix
        return CacheMiddleware.process_request(self, request)