<commit_before><?php

namespace LocaleDetector;

use Locale;
use Zend\EventManager\EventInterface;
use Zend\Mvc\MvcEvent;
use Zend\Http\PhpEnvironment\Request as HttpRequest;

class Module {

    public function getConfig()
    {
        return include __DIR__ . '/../config/module.config.php';
    }

    public function onBootstrap(EventInterface $e)
    {
        $app = $e->getApplication();
        $em = $app->getEventManager();
        $em->attach(MvcEvent::EVENT_ROUTE, array($this,'detectLocale'), PHP_INT_MAX);
    }

    public function detectLocale(MvcEvent $e)
    {
        $app = $e->getApplication();
        $sm  = $app->getServiceManager();
        $request = $app->getRequest();
        
        if (!$request instanceOf HttpRequest)
            return;

        $uri = $request->getUri();
        $localeConfig = $sm->get('configuration')['locale-detector'];
        $locale = $localeConfig['default-locale'];
        $requestUri = explode('/', $request->getRequestUri());
        $translator =  $sm->get('translator');

        if (!in_array($requestUri[1], $localeConfig['locales']))
        {
            $requestUri[0] = $locale;
            $url = implode('/', $requestUri);
            $response = $e->getResponse();
            $response->getHeaders()->addHeaderLine('Location', $url);
            $response->setStatusCode(302);
            $response->sendHeaders();
            // When an MvcEvent Listener returns a Response object,
            // It automatically short-circuit the Application running 
            // -> true only for Route Event propagation see Zend\Mvc\Application::run

            // To avoid additional processing
            // we can attach a listener for Event Route with a high priority<commit_msg>Fix a Locale Detector redirect bug<commit_after><?php

namespace LocaleDetector;

use Locale;
use Zend\EventManager\EventInterface;
use Zend\Mvc\MvcEvent;
use Zend\Http\PhpEnvironment\Request as HttpRequest;

class Module {

    public function getConfig()
    {
        return include __DIR__ . '/../config/module.config.php';
    }

    public function onBootstrap(EventInterface $e)
    {
        $app = $e->getApplication();
        $em = $app->getEventManager();
        $em->attach(MvcEvent::EVENT_ROUTE, array($this,'detectLocale'), PHP_INT_MAX);
    }

    public function detectLocale(MvcEvent $e)
    {
        $app = $e->getApplication();
        $sm  = $app->getServiceManager();
        $request = $app->getRequest();
        
        if (!$request instanceOf HttpRequest)
            return;

        $uri = $request->getUri();
        $localeConfig = $sm->get('configuration')['locale-detector'];
        $locale = $localeConfig['default-locale'];
        $requestUri = explode('/', $request->getRequestUri());
        $translator =  $sm->get('translator');

        if (!in_array($requestUri[1], $localeConfig['locales']))
        {
            $requestUri[0] = $locale;
            $url = '/'.implode('/', $requestUri);
            $response = $e->getResponse();
            $response->getHeaders()->addHeaderLine('Location', $url);
            $response->setStatusCode(302);
            $response->sendHeaders();
            // When an MvcEvent Listener returns a Response object,
            // It automatically short-circuit the Application running 
            // -> true only for Route Event propagation see Zend\Mvc\Application::run

            // To avoid additional processing
            // we can attach a listener for Event Route with a high priority
{'fraction_non_alphanumeric': 0.13812623832437024, 'fraction_numerical': 0.002830455703368242, 'mean_word_length': 2.492094861660079, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '6868150', 'n_tokens_mistral': 966, 'n_tokens_neox': 953, 'n_words': 226}