<commit_before><?php

namespace TypiCMS\Modules\Core\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use TypiCMS\Modules\Core\Facades\TypiCMS;

class SetLocale
{
    /**
     * Handle an incoming request.
     *
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $locale = config('app.locale');
        $firstSegment = $request->segment(1);
        if (in_array($firstSegment, locales())) {
            $locale = $firstSegment;
            App::setLocale($locale);
        }

        // Add locale prefix to URL if required.
        if (
            $firstSegment !== 'admin' &&
            !in_array($firstSegment, locales()) &&
            config('typicms.main_locale_in_url')
        ) {
            $segments = $request->segments();
            $segments = Arr::prepend($segments, $this->getBrowserLanguageOrDefault());

            return redirect()->to(implode('/', $segments));
        }

        // Not reliable, to be refactored.
        $localeAndCountry = $locale.'_'.mb_strtoupper($locale);

        setlocale(LC_ALL, $localeAndCountry.'.utf8', $localeAndCountry.'.utf-8', $localeAndCountry.'.UTF-8', $localeAndCountry);

        // Throw a 404 if website in this language is offline
        if ($firstSegment !== 'admin' && !config('typicms.'.$locale.'.status')) {
            abort(404);
        }<commit_msg>Update SetLocale.php<commit_after><?php

namespace TypiCMS\Modules\Core\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use TypiCMS\Modules\Core\Facades\TypiCMS;

class SetLocale
{
    /**
     * Handle an incoming request.
     *
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $locale = App::getLocale();
        $firstSegment = $request->segment(1);
        if (in_array($firstSegment, TypiCMS::enabledLocales())) {
            $locale = $firstSegment;
            App::setLocale($locale);
        }

        // Add locale prefix to URL if required.
        if (
            $firstSegment !== 'admin' &&
            !in_array($firstSegment, TypiCMS::enabledLocales()) &&
            config('typicms.main_locale_in_url')
        ) {
            $segments = $request->segments();
            $segments = Arr::prepend($segments, $this->getBrowserLanguageOrDefault());

            return redirect()->to(implode('/', $segments));
        }

        // Not reliable, to be refactored.
        $localeAndCountry = $locale.'_'.mb_strtoupper($locale);

        setlocale(LC_ALL, $localeAndCountry.'.utf8', $localeAndCountry.'.utf-8', $localeAndCountry.'.UTF-8', $localeAndCountry);

        // Throw a 404 if website in this language is offline
        if ($firstSegment !== 'admin' && !config('typicms.'.$locale.'.status')) {
            abort(404);
        }
{'fraction_non_alphanumeric': 0.1441965729065632, 'fraction_numerical': 0.006466214031684449, 'mean_word_length': 2.589327146171694, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '4492330', 'n_tokens_mistral': 926, 'n_tokens_neox': 922, 'n_words': 179}