# Path: easymode/utils/languagecode.py
# def get_short_language_codes():
#     """
#     Retrieves the short versions of the language codes defined in settings.LANGUAGES.
# 
#     >>> from django.conf import settings
#     >>> settings.LANGUAGES = (('en','English'),('de','German'),('nl-be','Belgium dutch'),('fr-be','Belgium french'),)
#     >>> sorted( get_short_language_codes() )
#     ['de', 'en', 'fr', 'nl']
#     
#     :rtype: A :class:`list` of short versions of the language codes.
#     """
#     languages = set([lang[:2] for lang in get_language_codes()])
#     return list(languages)
# 
# def get_language_code_from_shorthand(short_locale):
#     """
#     Returns the real language_code based on the shorthand.
#     
#     >>> settings.LANGUAGES = (('en-us','English'),('de','German'),('nl-be','Belgium dutch'),('fr-be','Belgium french'),)
#     >>> get_language_code_from_shorthand('en')
#     'en-us'
#     
#     :param short_locale: The short version of a language code.
#     :rtype: The long version of the language code.
#     """
#     for locale in get_language_codes():
#         if locale[:2] == short_locale:
#             return locale
#     return settings.LANGUAGE_CODE
# 
# def get_shorthand_from_language_code(locale):
#     """
#     Returns the shorthand, based on the language code.
#     
#     If USE_SHORT_LANGUAGE_CODES is true it will return a
#     ahorthand, if it is False, it will not modify the code
#     at all.
#     
#     >>> USE_SHORT_LANGUAGE_CODES = True
#     >>> get_shorthand_from_language_code('en-us')
#     'en'
#     >>> USE_SHORT_LANGUAGE_CODES = False
#     >>> get_shorthand_from_language_code('en-us')
#     'en-us'
#     
#     :param locale: The language code as a :class:`unicode` string.
#     :rtype: The short version of the language code (when appropriate).
#     """
#     if USE_SHORT_LANGUAGE_CODES:
#         return locale[:2]
#         
#     return locale


import re
import time
from django.conf import settings
from django.contrib.sessions.middleware import SessionMiddleware
from django.middleware.locale import LocaleMiddleware
from django.utils import translation
from django.utils.http import cookie_date
from easymode.utils.languagecode import get_short_language_codes,\
    get_language_code_from_shorthand,\
    get_shorthand_from_language_code as language_as_slug
    but it works.
    
    Don't put any vary header for the Accept-Language because the language does
    not depend on the vary header, the language is in the url.
    """
    
    def process_response(self, request, response):
        translation.deactivate()
        path = unicode(request.path)

        if not path.startswith(settings.MEDIA_URL) and \
            not path.startswith(settings.ADMIN_MEDIA_PREFIX) and \
            response.status_code == 200 and \
            response._headers['content-type'][1].split(';')[0] == "text/html":
                
            response.content = HREF_REGEX.sub(
                ur'<a\1href="/%s/\3"\4>' % language_as_slug(request.LANGUAGE_CODE), 
                response.content.decode('utf-8'))
            response.content = FORM_REGEX.sub(
                ur'<form\1action="/%s/\3"\4>' % language_as_slug(request.LANGUAGE_CODE), 
                response.content.decode('utf-8'))
                
        if (response.status_code == 301 or response.status_code == 302 ):
            location = response._headers['location']
            prefix = has_lang_prefix(location[1])
            if not prefix and location[1].startswith("/") and \
               not location[1].startswith(settings.MEDIA_URL) and \
               not location[1].startswith(settings.ADMIN_MEDIA_PREFIX):
                response._headers['location'] = (
                    location[0], "/%s%s" % (

# Path: easymode/utils/languagecode.py
# def get_short_language_codes():
#     """
#     Retrieves the short versions of the language codes defined in settings.LANGUAGES.
# 
#     >>> from django.conf import settings
#     >>> settings.LANGUAGES = (('en','English'),('de','German'),('nl-be','Belgium dutch'),('fr-be','Belgium french'),)
#     >>> sorted( get_short_language_codes() )
#     ['de', 'en', 'fr', 'nl']
#     
#     :rtype: A :class:`list` of short versions of the language codes.
#     """
#     languages = set([lang[:2] for lang in get_language_codes()])
#     return list(languages)
# 
# def get_language_code_from_shorthand(short_locale):
#     """
#     Returns the real language_code based on the shorthand.
#     
#     >>> settings.LANGUAGES = (('en-us','English'),('de','German'),('nl-be','Belgium dutch'),('fr-be','Belgium french'),)
#     >>> get_language_code_from_shorthand('en')
#     'en-us'
#     
#     :param short_locale: The short version of a language code.
#     :rtype: The long version of the language code.
#     """
#     for locale in get_language_codes():
#         if locale[:2] == short_locale:
#             return locale
#     return settings.LANGUAGE_CODE
# 
# def get_shorthand_from_language_code(locale):
#     """
#     Returns the shorthand, based on the language code.
#     
#     If USE_SHORT_LANGUAGE_CODES is true it will return a
#     ahorthand, if it is False, it will not modify the code
#     at all.
#     
#     >>> USE_SHORT_LANGUAGE_CODES = True
#     >>> get_shorthand_from_language_code('en-us')
#     'en'
#     >>> USE_SHORT_LANGUAGE_CODES = False
#     >>> get_shorthand_from_language_code('en-us')
#     'en-us'
#     
#     :param locale: The language code as a :class:`unicode` string.
#     :rtype: The short version of the language code (when appropriate).
#     """
#     if USE_SHORT_LANGUAGE_CODES:
#         return locale[:2]
#         
#     return locale


# Path: easymode/middleware.py
import re
import time
from django.conf import settings
from django.contrib.sessions.middleware import SessionMiddleware
from django.middleware.locale import LocaleMiddleware
from django.utils import translation
from django.utils.http import cookie_date
from easymode.utils.languagecode import get_short_language_codes,\
    get_language_code_from_shorthand,\
    get_shorthand_from_language_code as language_as_slug

    but it works.
    
    Don't put any vary header for the Accept-Language because the language does
    not depend on the vary header, the language is in the url.
    """
    
    def process_response(self, request, response):
        translation.deactivate()
        path = unicode(request.path)

        if not path.startswith(settings.MEDIA_URL) and \
            not path.startswith(settings.ADMIN_MEDIA_PREFIX) and \
            response.status_code == 200 and \
            response._headers['content-type'][1].split(';')[0] == "text/html":
                
            response.content = HREF_REGEX.sub(
                ur'<a\1href="/%s/\3"\4>' % language_as_slug(request.LANGUAGE_CODE), 
                response.content.decode('utf-8'))
            response.content = FORM_REGEX.sub(
                ur'<form\1action="/%s/\3"\4>' % language_as_slug(request.LANGUAGE_CODE), 
                response.content.decode('utf-8'))
                
        if (response.status_code == 301 or response.status_code == 302 ):
            location = response._headers['location']
            prefix = has_lang_prefix(location[1])
            if not prefix and location[1].startswith("/") and \
               not location[1].startswith(settings.MEDIA_URL) and \
               not location[1].startswith(settings.ADMIN_MEDIA_PREFIX):
                response._headers['location'] = (
                    location[0], "/%s%s" % (

                        language_as_slug(request.LANGUAGE_CODE), location[1]