public function setActiveFromUri($uri)
    {
        $regex = '/(^\/(' . $this->getAvailable() . '))(?:\/|\?|$)/i';

        // if languages set
        if ($this->enabled()) {
            // Try setting language from prefix of URL (/en/blah/blah).
            if (preg_match($regex, $uri, $matches)) {
                $this->lang_in_url = true;
                $this->active = $matches[2];
                $uri = preg_replace("/\\" . $matches[1] . '/', '', $uri, 1);

                // Store in session if language is different.
                if (isset($this->grav['session']) && $this->grav['session']->isStarted()
                    && $this->config->get('system.languages.session_store_active', true)
                    && $this->grav['session']->active_language != $this->active
                ) {
                    $this->grav['session']->active_language = $this->active;
                }
            } else {
                // Try getting language from the session, else no active.
                if (isset($this->grav['session']) && $this->grav['session']->isStarted() &&
                    $this->config->get('system.languages.session_store_active', true)) {
                    $this->active = $this->grav['session']->active_language ?: null;
                }
                // if still null, try from http_accept_language header
                if ($this->active === null &&
                    $this->config->get('system.languages.http_accept_language') &&
                    $accept = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? false) {

                    $negotiator = new LanguageNegotiator();
                    $best_language = $negotiator->getBest($accept, $this->languages);

                    if ($best_language instanceof AcceptLanguage) {
                        $this->active = $best_language->getType();
                    } else {
                        $this->active = $this->getDefault();
                    }

                }
            }
        }

        return $uri;
    }
public function setActiveFromUri($uri)
    {
        $regex = '/(^\/(' . $this->getAvailable() . '))(?:\/|\?|$)/i';

        // if languages set
        if ($this->enabled()) {
            // Try setting language from prefix of URL (/en/blah/blah).
            if (preg_match($regex, $uri, $matches)) {
                $this->lang_in_url = true;
                $this->active = $matches[2];
                $uri = preg_replace("/\\" . $matches[1] . '/', '', $uri, 1);

                // Store in session if language is different.
                if (isset($this->grav['session']) && $this->grav['session']->isStarted()
                    && $this->config->get('system.languages.session_store_active', true)
                    && $this->grav['session']->active_language != $this->active
                ) {
                    $this->grav['session']->active_language = $this->active;
                }
            } else {
                // Try getting language from the session, else no active.
                if (isset($this->grav['session']) && $this->grav['session']->isStarted() &&
                    $this->config->get('system.languages.session_store_active', true)) {
                    $this->active = $this->grav['session']->active_language ?: null;
                }
                // if still null, try from http_accept_language header
                if ($this->active === null &&
                    $this->config->get('system.languages.http_accept_language') &&
                    $accept = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? false) {

                    $negotiator = new LanguageNegotiator();
                    $best_language = $negotiator->getBest($accept, $this->languages);

                    if ($best_language instanceof AcceptLanguage) {
                        $this->active = $best_language->getType();
                    } else {
                        $this->active = $this->getDefault();
                    }

                }
            }
        }

        return $uri;
    }
public function setActiveFromUri ( $ uri ) { $ regex = '/(^\/(' . $ this -> getAvailable ( ) . '))(?:\/|\?|$)/i' ; // if languages set if ( $ this -> enabled ( ) ) { // Try setting language from prefix of URL (/en/blah/blah). if ( preg_match ( $ regex , $ uri , $ matches ) ) { $ this -> lang_in_url = true ; $ this -> active = $ matches [ 2 ] ; $ uri = preg_replace ( "/\\" . $ matches [ 1 ] . '/' , '' , $ uri , 1 ) ; // Store in session if language is different. if ( isset ( $ this -> grav [ 'session' ] ) && $ this -> grav [ 'session' ] -> isStarted ( ) && $ this -> config -> get ( 'system.languages.session_store_active' , true ) && $ this -> grav [ 'session' ] -> active_language != $ this -> active ) { $ this -> grav [ 'session' ] -> active_language = $ this -> active ; } } else { // Try getting language from the session, else no active. if ( isset ( $ this -> grav [ 'session' ] ) && $ this -> grav [ 'session' ] -> isStarted ( ) && $ this -> config -> get ( 'system.languages.session_store_active' , true ) ) { $ this -> active = $ this -> grav [ 'session' ] -> active_language ? : null ; } // if still null, try from http_accept_language header if ( $ this -> active === null && $ this -> config -> get ( 'system.languages.http_accept_language' ) && $ accept = $ _SERVER [ 'HTTP_ACCEPT_LANGUAGE' ] ?? false ) { $ negotiator = new LanguageNegotiator ( ) ; $ best_language = $ negotiator -> getBest ( $ accept , $ this -> languages ) ; if ( $ best_language instanceof AcceptLanguage ) { $ this -> active = $ best_language -> getType ( ) ; } else { $ this -> active = $ this -> getDefault ( ) ; } } } } return $ uri ; }
Sets the active language based on the first part of the URL

@param string $uri

@return string
Sets the active language based on the first part of the URL