<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class Locale
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        try {
            $locale = explode('-', explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'])[0])[0];
            if (in_array($locale, config('app.locales'))) {
                session(['browser-locale' => $locale]);
            }
        } catch (\Exception $e) {
        }
        app()->setLocale(session('locale', session('browser-locale', config('app.settings.language', config('app.locale', 'en')))));
        return $next($request);
    }
}
