# 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
        )
    )
else:
    HREF_REGEX = re.compile(
        ur'<a([^>]+)href="/(?!(%s|%s|%s))([^"]*)"([^>]*)>' % (
            "|".join(map(lambda l: l[0] + "/" , settings.LANGUAGES)), 
            settings.MEDIA_URL[1:], 
            settings.ADMIN_MEDIA_PREFIX[1:]
        )
    )
    FORM_REGEX = re.compile(
        ur'<form([^>]+)action="/(?!(%s|%s|%s))([^"]*)"([^>]*)>' % (
            "|".join(map(lambda l: l[0] + "/" , settings.LANGUAGES)),
             settings.MEDIA_URL[1:],
             settings.ADMIN_MEDIA_PREFIX[1:]
        )
    )

################################################################################
# helper functions
################################################################################

def has_lang_prefix(path):
    if USE_SHORT_LANGUAGE_CODES:
        check = MATCH_SHORT_LANGUAGE_CODE.match(path)
        
        if check is not None:
            return get_language_code_from_shorthand(check.group(1))
    else:
        check = MATCH_LANGUAGE_CODE.match(path)

# 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

        )
    )
else:
    HREF_REGEX = re.compile(
        ur'<a([^>]+)href="/(?!(%s|%s|%s))([^"]*)"([^>]*)>' % (
            "|".join(map(lambda l: l[0] + "/" , settings.LANGUAGES)), 
            settings.MEDIA_URL[1:], 
            settings.ADMIN_MEDIA_PREFIX[1:]
        )
    )
    FORM_REGEX = re.compile(
        ur'<form([^>]+)action="/(?!(%s|%s|%s))([^"]*)"([^>]*)>' % (
            "|".join(map(lambda l: l[0] + "/" , settings.LANGUAGES)),
             settings.MEDIA_URL[1:],
             settings.ADMIN_MEDIA_PREFIX[1:]
        )
    )

################################################################################
# helper functions
################################################################################

def has_lang_prefix(path):
    if USE_SHORT_LANGUAGE_CODES:
        check = MATCH_SHORT_LANGUAGE_CODE.match(path)
        
        if check is not None:
            return get_language_code_from_shorthand(check.group(1))
    else:
        check = MATCH_LANGUAGE_CODE.match(path)

        if check is not None: