public function negotiateLanguage()
    {
        $matches = $this->getMatchesFromAcceptedLanguages();
        foreach ($matches as $key => $q) {

            $key = ($this->configRepository->get('laravellocalization.localesMapping')[$key]) ?? $key;

            if (!empty($this->supportedLanguages[$key])) {
                return $key;
            }

            if ($this->use_intl) {
                $key = Locale::canonicalize($key);
            }

            // Search for acceptable locale by 'regional' => 'af_ZA' or 'lang' => 'af-ZA' match.
            foreach ( $this->supportedLanguages as $key_supported => $locale ) {
                if ( (isset($locale['regional']) && $locale['regional'] == $key) || (isset($locale['lang']) && $locale['lang'] == $key) ) {
                    return $key_supported;
                }
            }
        }
        // If any (i.e. "*") is acceptable, return the first supported format
        if (isset($matches['*'])) {
            reset($this->supportedLanguages);

            return key($this->supportedLanguages);
        }

        if ($this->use_intl && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            $http_accept_language = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);

            if (!empty($this->supportedLanguages[$http_accept_language])) {
                return $http_accept_language;
            }
        }

        if ($this->request->server('REMOTE_HOST')) {
            $remote_host = explode('.', $this->request->server('REMOTE_HOST'));
            $lang = strtolower(end($remote_host));

            if (!empty($this->supportedLanguages[$lang])) {
                return $lang;
            }
        }

        return $this->defaultLocale;
    }
public function negotiateLanguage()
    {
        $matches = $this->getMatchesFromAcceptedLanguages();
        foreach ($matches as $key => $q) {

            $key = ($this->configRepository->get('laravellocalization.localesMapping')[$key]) ?? $key;

            if (!empty($this->supportedLanguages[$key])) {
                return $key;
            }

            if ($this->use_intl) {
                $key = Locale::canonicalize($key);
            }

            // Search for acceptable locale by 'regional' => 'af_ZA' or 'lang' => 'af-ZA' match.
            foreach ( $this->supportedLanguages as $key_supported => $locale ) {
                if ( (isset($locale['regional']) && $locale['regional'] == $key) || (isset($locale['lang']) && $locale['lang'] == $key) ) {
                    return $key_supported;
                }
            }
        }
        // If any (i.e. "*") is acceptable, return the first supported format
        if (isset($matches['*'])) {
            reset($this->supportedLanguages);

            return key($this->supportedLanguages);
        }

        if ($this->use_intl && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            $http_accept_language = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);

            if (!empty($this->supportedLanguages[$http_accept_language])) {
                return $http_accept_language;
            }
        }

        if ($this->request->server('REMOTE_HOST')) {
            $remote_host = explode('.', $this->request->server('REMOTE_HOST'));
            $lang = strtolower(end($remote_host));

            if (!empty($this->supportedLanguages[$lang])) {
                return $lang;
            }
        }

        return $this->defaultLocale;
    }
public function negotiateLanguage ( ) { $ matches = $ this -> getMatchesFromAcceptedLanguages ( ) ; foreach ( $ matches as $ key => $ q ) { $ key = ( $ this -> configRepository -> get ( 'laravellocalization.localesMapping' ) [ $ key ] ) ?? $ key ; if ( ! empty ( $ this -> supportedLanguages [ $ key ] ) ) { return $ key ; } if ( $ this -> use_intl ) { $ key = Locale :: canonicalize ( $ key ) ; } // Search for acceptable locale by 'regional' => 'af_ZA' or 'lang' => 'af-ZA' match. foreach ( $ this -> supportedLanguages as $ key_supported => $ locale ) { if ( ( isset ( $ locale [ 'regional' ] ) && $ locale [ 'regional' ] == $ key ) || ( isset ( $ locale [ 'lang' ] ) && $ locale [ 'lang' ] == $ key ) ) { return $ key_supported ; } } } // If any (i.e. "*") is acceptable, return the first supported format if ( isset ( $ matches [ '*' ] ) ) { reset ( $ this -> supportedLanguages ) ; return key ( $ this -> supportedLanguages ) ; } if ( $ this -> use_intl && ! empty ( $ _SERVER [ 'HTTP_ACCEPT_LANGUAGE' ] ) ) { $ http_accept_language = Locale :: acceptFromHttp ( $ _SERVER [ 'HTTP_ACCEPT_LANGUAGE' ] ) ; if ( ! empty ( $ this -> supportedLanguages [ $ http_accept_language ] ) ) { return $ http_accept_language ; } } if ( $ this -> request -> server ( 'REMOTE_HOST' ) ) { $ remote_host = explode ( '.' , $ this -> request -> server ( 'REMOTE_HOST' ) ) ; $ lang = strtolower ( end ( $ remote_host ) ) ; if ( ! empty ( $ this -> supportedLanguages [ $ lang ] ) ) { return $ lang ; } } return $ this -> defaultLocale ; }
Negotiates language with the user's browser through the Accept-Language
HTTP header or the user's host address.  Language codes are generally in
the form "ll" for a language spoken in only one country, or "ll-CC" for a
language spoken in a particular country.  For example, U.S. English is
"en-US", while British English is "en-UK".  Portuguese as spoken in
Portugal is "pt-PT", while Brazilian Portuguese is "pt-BR".

This function is based on negotiateLanguage from Pear HTTP2
http://pear.php.net/package/HTTP2/

Quality factors in the Accept-Language: header are supported, e.g.:
Accept-Language: en-UK;q=0.7, en-US;q=0.6, no, dk;q=0.8

@return string The negotiated language result or app.locale.
Negotiates language with the user s browser through the Accept - Language HTTP header or the user s host address . Language codes are generally in the form ll for a language spoken in only one country or ll - CC for a language spoken in a particular country . For example U . S . English is en - US while British English is en - UK . Portuguese as spoken in Portugal is pt - PT while Brazilian Portuguese is pt - BR .