<commit_before><?php
namespace I18n\Routing\Filter;

use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\I18n\I18n;
use Cake\Network\Request;
use Cake\Routing\DispatcherFilter;

class I18nFilter extends DispatcherFilter
{
    /**
     * Default config.
     *
     * ### Valid keys
     *
     * - `detectLanguage`: If `true` will attempt to get browser locale and
     * redirect to similar language available in app when going to site root.
     * Default `false`.
     *
     * @var array
     */
    protected $_defaultConfig = [
        'detectLanguage' => false,
    ];

    /**
     * Set appropirate locale and lang to I18n::locale() and App.language config
     * respectively based on "lang" request param.
     *
     * {@inheritDoc}
     */
    public function beforeDispatch(Event $event)
    {
        $request = $event->data['request'];

        if (empty($request->url)) {
            $response = $event->data['response'];
            $event->stopPropagation();

            $statusCode = 301;
            $lang = Configure::read('I18n.defaultLanguage');
            if ($this->config('detectLanguage')) {
                $statusCode = 302;
                $lang = $this->detectLanguage($request);
            }

            $response->statusCode($statusCode);
            $response->header('Location', $request->webroot . $lang);

            return $response;
        }

        $langs = Configure::read('I18n.languages');
        $lang = $request->param('lang');
        if (isset($langs[$lang])) {
            I18n::locale($langs[$lang]['locale']);
            Configure::write('App.language', $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\Network\Request $request Request instance.
     * @param string|null $default Default language to return if no match is found.
     *
     * @return string
     */
    public function detectLanguage(Request $request, $default = null)
    {
        if (empty($default)) {
            $lang = Configure::read('I18n.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(Configure::read('I18n.languages'))
        );
        if (!empty($acceptedLangs)) {
            $lang = reset($acceptedLangs);
        }

        return $lang;
    }
}<commit_msg>Update I18nFilter to take config through constructor.<commit_after><?php
namespace ADmad\I18n\Routing\Filter;

use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\I18n\I18n;
use Cake\Network\Request;
use Cake\Routing\DispatcherFilter;
use Cake\Utility\Hash;

class I18nFilter extends DispatcherFilter
{
    /**
     * Default config.
     *
     * ### Valid keys
     *
     * - `detectLanguage`: If `true` will attempt to get browser locale and
     * redirect to similar language available in app when going to site root.
     * Default `false`.
     * - `defaultLanguage`: Default language for app. Default `en_US`.
     * - `availableLanguages`: Languages available in app. Default `[]`.
     *
     * @var array
     */
    protected $_defaultConfig = [
        'detectLanguage' => false,
        'defaultLanguage' => 'en_US',
        'availableLanguages' => []
    ];

    /**
     * Constructor.
     *
     * @param array $config Settings for the filter.
     */
    public function __construct($config = [])
    {
        if (isset($config['availableLanguages'])) {
            $config['availableLanguages'] = Hash::normalize($config['availableLanguages']);
        }

        parent::__construct($config);
    }

    /**
     * Callback for Dispatcher.beforeDispatch event.
     *
     * Sets appropriate locale and lang to I18n::locale() and App.language config
     * respectively based on "lang" request param.
     *
     * @param \Cake\Event\Event $event Event object.
     * @return \Cake\Network\Response|null
     */
    public function beforeDispatch(Event $event)
    {
        $request = $event->data['request'];

        if (empty($request->url)) {
            $response = $event->data['response'];
            $event->stopPropagation();

            $statusCode = 301;
            $lang = $this->_config['defaultLanguage'];
            if ($this->_config['detectLanguage']) {
                $statusCode = 302;
                $lang = $this->detectLanguage($request, $lang);
            }

            $response->statusCode($statusCode);
            $response->header('Location', $request->webroot . $lang);

            return $response;
        }

        $langs = $this->_config['availableLanguages'];
        $lang = $request->param('lang') ?: $this->_config['defaultLanguage'];
        if (isset($langs[$lang])) {
            I18n::locale($langs[$lang]['locale']);
        } else {
            I18n::locale($lang);
        }

        Configure::write('App.language', $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\Network\Request $request Request instance.
     * @param string|null $default Default language to return if no match is found.
     *
     * @return string
     */
    public function detectLanguage(Request $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['availableLanguages'])
        );
        if (!empty($acceptedLangs)) {
            $lang = reset($acceptedLangs);
        }

        return $lang;
    }
}
{'fraction_non_alphanumeric': 0.134431783184478, 'fraction_numerical': 0.009239297813366184, 'mean_word_length': 2.1271064034665383, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 36, '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': '4900405', 'n_tokens_mistral': 1878, 'n_tokens_neox': 1802, 'n_words': 438}