<commit_before><?php
namespace Phwoolcon;

use Phalcon\Di;
use Phalcon\Events\Event;
use Phalcon\Translate\Adapter;

class I18n extends Adapter
{
    /**
     * @var Di
     */
    protected static $di;
    /**
     * @var static
     */
    protected static $instance;
    protected $locale = [];
    protected $localePath;
    protected $defaultLocale;
    protected $currentLocale;
    protected $multiLocale = false;
    protected $detectClientLocale = false;
    protected $options = [];
    protected $availableLocales = [];
    /**
     * @var \Phalcon\Http\Request
     */
    protected $request;

    public function __construct(array $options = [])
    {
        parent::__construct($options);
        $this->multiLocale = $options['multi_locale'];
        $this->multiLocale and $this->availableLocales = $options['available_locales'];
        $this->detectClientLocale = $options['detect_client_locale'];
        $this->defaultLocale = $this->currentLocale = $options['default_locale'];
        $this->localePath = $options['locale_path'];
        $this->options = $options;
        $this->loadLocale($this->defaultLocale);
        $this->_reset();
    }

    protected function _reset()
    {
        if (!$this->multiLocale) {
            return;
        }
        $this->request or $this->request = static::$di->getShared('request');
        if ($locale = $this->request->get('_locale')) {
            $this->_setLocale($locale);
            return;
        }
        if (($cookie = Cookies::get('locale')) && $locale = $cookie->useEncryption(false)->getValue()) {
            $this->_setLocale($locale);
            $cookie->setHttpOnly(false)
                ->delete();
            return;
        }
        if ($locale = Session::get('current_locale')) {
            $this->loadLocale($this->currentLocale = $locale);
            return;
        }
        if ($this->detectClientLocale) {
            $this->_setLocale($this->request->getBestLanguage());
        }
    }

    protected function _setLocale($locale)
    {
        if (strpos($locale, '..') || !is_dir($this->localePath . '/' . $locale)) {
            $locale = $this->defaultLocale;
        }
        Session::set('current_locale', $this->currentLocale = $locale);
        $this->loadLocale($locale);
    }

    public static function checkMobile($mobile, $country = 'CN')
    {
        static::$instance or static::$instance = static::$di->getShared('i18n');
        $pattern = fnGet(static::$instance->options, "verification_patterns.{$country}.mobile");
        return $pattern ? preg_match($pattern, $mobile) : true;
    }

    public static function clearCache()
    {
        static::$instance or static::$instance = static::$di->getShared('i18n');
        foreach (glob(static::$instance->localePath . '/*', GLOB_ONLYDIR) as $dir) {
            $locale = basename($dir);
            Cache::delete('locale.' . $locale);
        }
    }<commit_msg>fix(i18n): restore default locale if no client locale matched<commit_after><?php
namespace Phwoolcon;

use Phalcon\Di;
use Phalcon\Events\Event;
use Phalcon\Translate\Adapter;

class I18n extends Adapter
{
    /**
     * @var Di
     */
    protected static $di;
    /**
     * @var static
     */
    protected static $instance;
    protected $locale = [];
    protected $localePath;
    protected $defaultLocale;
    protected $currentLocale;
    protected $multiLocale = false;
    protected $detectClientLocale = false;
    protected $options = [];
    protected $availableLocales = [];
    /**
     * @var \Phalcon\Http\Request
     */
    protected $request;

    public function __construct(array $options = [])
    {
        parent::__construct($options);
        $this->multiLocale = $options['multi_locale'];
        $this->multiLocale and $this->availableLocales = $options['available_locales'];
        $this->detectClientLocale = $options['detect_client_locale'];
        $this->defaultLocale = $this->currentLocale = $options['default_locale'];
        $this->localePath = $options['locale_path'];
        $this->options = $options;
        $this->loadLocale($this->defaultLocale);
        $this->_reset();
    }

    protected function _reset()
    {
        if (!$this->multiLocale) {
            return;
        }
        $this->request or $this->request = static::$di->getShared('request');
        if ($locale = $this->request->get('_locale')) {
            $this->_setLocale($locale);
            return;
        }
        if (($cookie = Cookies::get('locale')) && $locale = $cookie->useEncryption(false)->getValue()) {
            $this->_setLocale($locale);
            $cookie->setHttpOnly(false)
                ->delete();
            return;
        }
        if ($locale = Session::get('current_locale')) {
            $this->loadLocale($this->currentLocale = $locale);
            return;
        }
        if ($this->detectClientLocale) {
            $this->_setLocale($this->request->getBestLanguage());
            return;
        }
        $this->loadLocale($this->currentLocale = $this->defaultLocale);
    }

    protected function _setLocale($locale)
    {
        if (strpos($locale, '..') || !is_dir($this->localePath . '/' . $locale)) {
            $locale = $this->defaultLocale;
        }
        Session::set('current_locale', $this->currentLocale = $locale);
        $this->loadLocale($locale);
    }

    public static function checkMobile($mobile, $country = 'CN')
    {
        static::$instance or static::$instance = static::$di->getShared('i18n');
        $pattern = fnGet(static::$instance->options, "verification_patterns.{$country}.mobile");
        return $pattern ? preg_match($pattern, $mobile) : true;
    }

    public static function clearCache()
    {
        static::$instance or static::$instance = static::$di->getShared('i18n');
        foreach (glob(static::$instance->localePath . '/*', GLOB_ONLYDIR) as $dir) {
            $locale = basename($dir);
            Cache::delete('locale.' . $locale);
        }
    }
{'fraction_non_alphanumeric': 0.15979381443298968, 'fraction_numerical': 0.002327901563019621, 'mean_word_length': 2.517543859649123, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 80, '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': '50719', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1681, 'n_words': 307}