<commit_before><?php

namespace Igniter\Flame\Translation;

use Carbon\Carbon;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Http\Request;

class Localization
{
    protected $request;

    protected $config;

    protected $sessionKey = 'igniter.translation.locale';

    public function __construct(Request $request, Repository $config)
    {
        $this->request = $request;
        $this->config = $config;
    }

    public function loadLocale()
    {
        $locale = $this->getLocale();

        if ($this->config['app.locale'] != $locale) {
            app()->setLocale($locale);
            Carbon::setLocale($locale);
        }
    }

    public function getLocale()
    {
        // Check request for locale
        $routeLocale = $this->getRouteLocale();
        if ($routeLocale AND $this->isValid($routeLocale)) {
            return $routeLocale;
        }

        // Get locale from session
        $sessionLocale = $this->getSessionLocale();
        if ($sessionLocale AND $this->isValid($sessionLocale)) {
            return $sessionLocale;
        }

        // Get locale from user browser
        if ($this->detectBrowserLocale()) {
            $browserLocale = $this->getBrowserLocale();
            if ($browserLocale AND $this->isValid($browserLocale)) {
                return $browserLocale;
            }
        }

        return $this->getConfig('locale');
    }

    public function supportedLocales()
    {
        return $this->getConfig('supportedLocales', []);
    }

    public function detectBrowserLocale()
    {
        return (bool)$this->getConfig('detectBrowserLocale', FALSE);
    }

    public function isValid($locale)
    {
        return in_array($locale, $this->supportedLocales());
    }

    public function setSessionLocale($locale)
    {
        return $this->request->getSession()->put([$this->sessionKey => $locale]);
    }

    public function getSessionLocale()
    {
        return $this->request->getSession()->get($this->sessionKey);
    }

    protected function getRouteLocale()
    {
        $paths = explode('/', $this->request->path());

        return $paths[0] ?? null;
    }

    protected function getBrowserLocale()
    {
        return substr($this->request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
    }

    protected function getConfig(string $string)
    {
        return $this->config['localization.'.$string];
    }
}<commit_msg>Detect locale in the user browser before the route and session<commit_after><?php

namespace Igniter\Flame\Translation;

use Carbon\Carbon;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Http\Request;

class Localization
{
    protected $request;

    protected $config;

    protected $sessionKey = 'igniter.translation.locale';

    public function __construct(Request $request, Repository $config)
    {
        $this->request = $request;
        $this->config = $config;
    }

    public function loadLocale()
    {
        $locale = $this->getLocale();

        if ($this->config['app.locale'] != $locale) {
            app()->setLocale($locale);
            Carbon::setLocale($locale);
        }
    }

    public function getLocale()
    {
        // Get locale from user browser
        if ($this->detectBrowserLocale()) {
            $browserLocale = $this->getBrowserLocale();
            if ($browserLocale AND $this->isValid($browserLocale)) {
                return $browserLocale;
            }
        }

        // Check request for locale
        $routeLocale = $this->getRouteLocale();
        if ($routeLocale AND $this->isValid($routeLocale)) {
            return $routeLocale;
        }

        // Get locale from session
        $sessionLocale = $this->getSessionLocale();
        if ($sessionLocale AND $this->isValid($sessionLocale)) {
            return $sessionLocale;
        }

        return $this->getConfig('locale');
    }

    public function supportedLocales()
    {
        return $this->getConfig('supportedLocales', []);
    }

    public function detectBrowserLocale()
    {
        return (bool)$this->getConfig('detectBrowserLocale', FALSE);
    }

    public function isValid($locale)
    {
        return in_array($locale, $this->supportedLocales());
    }

    public function setSessionLocale($locale)
    {
        return $this->request->getSession()->put([$this->sessionKey => $locale]);
    }

    public function getSessionLocale()
    {
        return $this->request->getSession()->get($this->sessionKey);
    }

    protected function getRouteLocale()
    {
        $paths = explode('/', $this->request->path());

        return $paths[0] ?? null;
    }

    protected function getBrowserLocale()
    {
        return substr($this->request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
    }

    protected function getConfig(string $string)
    {
        return $this->config['localization.'.$string];
    }
}
{'fraction_non_alphanumeric': 0.13464293022304072, 'fraction_numerical': 0.0012277470841006752, 'mean_word_length': 2.4544169611307423, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '6624447', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1295, 'n_words': 274}