<commit_before>import re

from django.conf import settings
from django.http import HttpResponseRedirect


# Matches things like
#   /en
#   /en/
#   /en/foo/bar (can be anything after the first trailing slash)
# But not
#   /en-gb
# because the fallback language code is not followed immediately by a slash.
FALLBACK_PREFIX_PATTERN = re.compile(
    r'^/(?P<lang>{langs})(?:/?|/.+)$'.format(
        langs='|'.join(settings.FALLBACK_LANGUAGE_PREFIXES.keys()),
    ),
    re.UNICODE,
)


class LocaleFallbackMiddleware:
    """Redirect entries in ``settings.FALLBACK_LANGUAGE_PREFIXES`` to a
    valid language prefix.
    """
    response_redirect_class = HttpResponseRedirect

    def process_request(self, request):
        if not settings.USE_I18N:
            return
        match = FALLBACK_PREFIX_PATTERN.match(request.path_info)
        if not match:
            return
        lang = match.group('lang')
        fallback = settings.FALLBACK_LANGUAGE_PREFIXES[lang]
        path = request.get_full_path().replace(lang, fallback, 1)
        return self.response_redirect_class(path)<commit_msg>Prepend script prefix when replacing lang code<commit_after>import re

from django.conf import settings
from django.core.urlresolvers import get_script_prefix
from django.http import HttpResponseRedirect


# Matches things like
#   /en
#   /en/
#   /en/foo/bar (can be anything after the first trailing slash)
# But not
#   /en-gb
# because the fallback language code is not followed immediately by a slash.
FALLBACK_PREFIX_PATTERN = re.compile(
    r'^/(?P<lang>{langs})(?:/?|/.+)$'.format(
        langs='|'.join(settings.FALLBACK_LANGUAGE_PREFIXES.keys()),
    ),
    re.UNICODE,
)


class LocaleFallbackMiddleware:
    """Redirect entries in ``settings.FALLBACK_LANGUAGE_PREFIXES`` to a
    valid language prefix.
    """
    response_redirect_class = HttpResponseRedirect

    def process_request(self, request):
        if not settings.USE_I18N:
            return
        match = FALLBACK_PREFIX_PATTERN.match(request.path_info)
        if not match:
            return
        lang = match.group('lang')
        fallback = settings.FALLBACK_LANGUAGE_PREFIXES[lang]
        script_prefix = get_script_prefix()
        path = request.get_full_path().replace(
            script_prefix + lang, script_prefix + fallback, 1,
        )
        return self.response_redirect_class(path)
{'fraction_non_alphanumeric': 0.09970674486803519, 'fraction_numerical': 0.0025136154168412233, 'mean_word_length': 3.1894736842105265, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5413741', 'n_tokens_mistral': 743, 'n_tokens_neox': 719, 'n_words': 174}