public function detectLanguage(ServerRequest $request, $default = null)
    {
        if (empty($default)) {
            $lang = $this->_config['defaultLanguage'];
        } else {
            $lang = $default;
        }

        $browserLangs = $request->acceptLanguage();
        foreach ($browserLangs as $k => $langKey) {
            if (strpos($langKey, '-') !== false) {
                $browserLangs[$k] = substr($langKey, 0, 2);
            }
        }
        $acceptedLangs = array_intersect(
            $browserLangs,
            array_keys($this->_config['languages'])
        );
        if (!empty($acceptedLangs)) {
            $lang = reset($acceptedLangs);
        }

        return $lang;
    }
public function detectLanguage(ServerRequest $request, $default = null)
    {
        if (empty($default)) {
            $lang = $this->_config['defaultLanguage'];
        } else {
            $lang = $default;
        }

        $browserLangs = $request->acceptLanguage();
        foreach ($browserLangs as $k => $langKey) {
            if (strpos($langKey, '-') !== false) {
                $browserLangs[$k] = substr($langKey, 0, 2);
            }
        }
        $acceptedLangs = array_intersect(
            $browserLangs,
            array_keys($this->_config['languages'])
        );
        if (!empty($acceptedLangs)) {
            $lang = reset($acceptedLangs);
        }

        return $lang;
    }
public function detectLanguage ( ServerRequest $ request , $ default = null ) { if ( empty ( $ default ) ) { $ lang = $ this -> _config [ 'defaultLanguage' ] ; } else { $ lang = $ default ; } $ browserLangs = $ request -> acceptLanguage ( ) ; foreach ( $ browserLangs as $ k => $ langKey ) { if ( strpos ( $ langKey , '-' ) !== false ) { $ browserLangs [ $ k ] = substr ( $ langKey , 0 , 2 ) ; } } $ acceptedLangs = array_intersect ( $ browserLangs , array_keys ( $ this -> _config [ 'languages' ] ) ) ; if ( ! empty ( $ acceptedLangs ) ) { $ lang = reset ( $ acceptedLangs ) ; } return $ lang ; }
Get languages accepted by browser and return the one matching one of
those in config var `I18n.languages`.

You should set config var `I18n.languages` to an array containing
languages available in your app.

@param \Cake\Http\ServerRequest $request The request.
@param string|null $default Default language to return if no match is found.

@return string
Get languages accepted by browser and return the one matching one of those in config var I18n . languages .