<commit_before><?php
/**
 * @copyright Copyright (C) 2015-2018 <NAME>
 * @license https://github.com/fetus-hina/stat.ink/blob/master/LICENSE MIT
 * @author <NAME> <<EMAIL>>
 */

namespace app\components\web;

use Yii;
use app\models\Language;
use app\models\Timezone;
use yii\base\Event;
use yii\base\View;
use yii\web\Controller as Base;
use yii\web\Cookie;

class Controller extends Base
{
    use HttpErrorTrait;

    public function init()
    {
        parent::init();
        $this->setLanguage();
        $this->setTimezone();
    }

    private function setLanguage(): void
    {
        $lang = (function (): ?Language {
            if ($val = $this->detectLanguageByParam()) {
                return $val;
            }

            if ($val = $this->detectLanguageByCookie()) {
                return $val;
            }

            if ($val = $this->detectLanguageByHeader()) {
                return $val;
            }

            return null;
        })();

        if ($lang) {
            Yii::$app->language = $lang->lang;
            if ($lang->di) {
                $di = is_string($lang->di) ? json_decode($lang->di, true) : $lang->di;
                if (is_array($di)) {
                    foreach ($di as $component => $data) {
                        foreach ($data as $key => $value) {
                            Yii::$app->$component->$key = $value;
                        }
                    }
                }
            }
        }
    }

    private function detectLanguageByParam(): ?Language
    {
        $param = Yii::$app->request->get('_lang_');
        if (!is_scalar($param)) {
            return null;
        }

        if (!$lang = Language::findOne(['lang' => (string)$param])) {
            return null;
        }

        if ($this->detectLanguageByCookie() === false) {
            Yii::$app->response->cookies->add(
                new Cookie([
                    'name' => 'language',
                    'value' => $lang->lang,
                    'expire' => time() + 86400 * 366,
                ])
            );
        }

        return $lang;
    }

    private function detectLanguageByCookie(): ?Language
    {
        $cookie = Yii::$app->request->cookies->get('language');
        if (!$cookie) {
            return null;
        }

        if (!$lang = Language::findOne(['lang' => $cookie->value])) {
            return null;
        }

        return $lang;
    }

    private function detectLanguageByHeader(): ?Language
    {
        $request = Yii::$app->request;
        $userAgent   = trim($request->userAgent);
        $acceptLangs = $request->acceptableLanguages;
        if (empty($acceptLangs) ||
                $userAgent == '' ||
                stripos($userAgent, 'bot') !== false ||
                stripos($userAgent, 'spider') !== false ||
                stripos($userAgent, 'google') !== false ||
                stripos($userAgent, 'http://') !== false ||
                stripos($userAgent, 'https://') !== false
        ) {
            return null;
        }

        //FIXME
        $firstLang = strtolower(array_shift($acceptLangs));
        $firstLangShort = substr($firstLang, 0, 2);
        switch ($firstLangShort) {
            case 'ja':
                return Language::findOne(['lang' => 'ja-JP']);

            case 'en':
                return Language::findOne([
                    'lang' => ($firstLang === 'en-gb' || $firstLang === 'en-au')
                        ? 'en-GB'
                        : 'en-US',
                ]);

            case 'es':
                return Language::findOne([
                    'lang' => $firstLang === 'es-es' ? 'es-ES' : 'es-MX',
                ]);

            case 'fr':
                return Language::findOne([
                    'lang' => $firstLang === 'fr-ca' ? 'fr-CA' : 'fr-FR',
                ]);

            case 'it':
                return Language::findOne(['lang' => 'it-IT']);

            case 'ru':
                return Language::findOne(['lang' => 'ru-RU']);

            case 'nl':
                return Language::findOne(['lang' => 'nl-NL']);

            default:
                return null;
        }
    }

    private function setTimezone()
    {
        $tz = (function () {
            $cookie = Yii::$app->request->cookies->get('timezone');
            if ($cookie) {
                $tz = Timezone::findOne(['identifier' => $cookie->value]);
                if ($tz) {
                    return $tz;
                }
            }
            switch (strtolower(Yii::$app->language)) {
                case 'en':
                case 'en-us':
                    return Timezone::findOne(['identifier' => 'America/Los_Angeles']);

                case 'en-gb':
                    return Timezone::findOne(['identifier' => 'Europe/London']);

                case 'es':
                case 'es-ES':
                case 'fr':
                case 'fr-FR':
                case 'it':
                case 'it-IT':
                case 'nl':
                case 'nl-NL':
                    return Timezone::findOne(['identifier' => 'Europe/Paris']);

                case 'es-MX':
                    return Timezone::findOne(['identifier' => 'America/Chicago']);

                case 'fr-CA':
                    return Timezone::findOne(['identifier' => 'America/Halifax']);

                case 'ru':
                case 'ru-RU':
                    return Timezone::findOne(['identifier' => 'Europe/Moscow']);

                default:
                    return Timezone::findOne(['identifier' => 'Asia/Tokyo']);
            }
        })();
        if ($tz) {
            Yii::$app->setTimeZone($tz->identifier);
            Yii::$app->setSplatoonRegion($tz->region_id);
        }
    }
}
<commit_msg>Set `ja-JP` instead of `ja-JP@calendar=japanese` to Yii::$app->language<commit_after><?php
/**
 * @copyright Copyright (C) 2015-2018 <NAME>
 * @license https://github.com/fetus-hina/stat.ink/blob/master/LICENSE MIT
 * @author <NAME> <<EMAIL>>
 */

namespace app\components\web;

use Yii;
use app\models\Language;
use app\models\Timezone;
use yii\base\Event;
use yii\base\View;
use yii\web\Controller as Base;
use yii\web\Cookie;

class Controller extends Base
{
    use HttpErrorTrait;

    public function init()
    {
        parent::init();
        $this->setLanguage();
        $this->setTimezone();
    }

    private function setLanguage(): void
    {
        $lang = (function (): ?Language {
            if ($val = $this->detectLanguageByParam()) {
                return $val;
            }

            if ($val = $this->detectLanguageByCookie()) {
                return $val;
            }

            if ($val = $this->detectLanguageByHeader()) {
                return $val;
            }

            return null;
        })();

        if ($lang) {
            Yii::$app->language = $lang->getLanguageId();
            if ($lang->di) {
                $di = is_string($lang->di) ? json_decode($lang->di, true) : $lang->di;
                if (is_array($di)) {
                    foreach ($di as $component => $data) {
                        foreach ($data as $key => $value) {
                            Yii::$app->$component->$key = $value;
                        }
                    }
                }
            }
        }
    }

    private function detectLanguageByParam(): ?Language
    {
        $param = Yii::$app->request->get('_lang_');
        if (!is_scalar($param)) {
            return null;
        }

        if (!$lang = Language::findOne(['lang' => (string)$param])) {
            return null;
        }

        if ($this->detectLanguageByCookie() === false) {
            Yii::$app->response->cookies->add(
                new Cookie([
                    'name' => 'language',
                    'value' => $lang->lang,
                    'expire' => time() + 86400 * 366,
                ])
            );
        }

        return $lang;
    }

    private function detectLanguageByCookie(): ?Language
    {
        $cookie = Yii::$app->request->cookies->get('language');
        if (!$cookie) {
            return null;
        }

        if (!$lang = Language::findOne(['lang' => $cookie->value])) {
            return null;
        }

        return $lang;
    }

    private function detectLanguageByHeader(): ?Language
    {
        $request = Yii::$app->request;
        $userAgent   = trim($request->userAgent);
        $acceptLangs = $request->acceptableLanguages;
        if (empty($acceptLangs) ||
                $userAgent == '' ||
                stripos($userAgent, 'bot') !== false ||
                stripos($userAgent, 'spider') !== false ||
                stripos($userAgent, 'google') !== false ||
                stripos($userAgent, 'http://') !== false ||
                stripos($userAgent, 'https://') !== false
        ) {
            return null;
        }

        //FIXME
        $firstLang = strtolower(array_shift($acceptLangs));
        $firstLangShort = substr($firstLang, 0, 2);
        switch ($firstLangShort) {
            case 'ja':
                return Language::findOne(['lang' => 'ja-JP']);

            case 'en':
                return Language::findOne([
                    'lang' => ($firstLang === 'en-gb' || $firstLang === 'en-au')
                        ? 'en-GB'
                        : 'en-US',
                ]);

            case 'es':
                return Language::findOne([
                    'lang' => $firstLang === 'es-es' ? 'es-ES' : 'es-MX',
                ]);

            case 'fr':
                return Language::findOne([
                    'lang' => $firstLang === 'fr-ca' ? 'fr-CA' : 'fr-FR',
                ]);

            case 'it':
                return Language::findOne(['lang' => 'it-IT']);

            case 'ru':
                return Language::findOne(['lang' => 'ru-RU']);

            case 'nl':
                return Language::findOne(['lang' => 'nl-NL']);

            default:
                return null;
        }
    }

    private function setTimezone()
    {
        $tz = (function () {
            $cookie = Yii::$app->request->cookies->get('timezone');
            if ($cookie) {
                $tz = Timezone::findOne(['identifier' => $cookie->value]);
                if ($tz) {
                    return $tz;
                }
            }
            switch (strtolower(Yii::$app->language)) {
                case 'en':
                case 'en-us':
                    return Timezone::findOne(['identifier' => 'America/Los_Angeles']);

                case 'en-gb':
                    return Timezone::findOne(['identifier' => 'Europe/London']);

                case 'es':
                case 'es-ES':
                case 'fr':
                case 'fr-FR':
                case 'it':
                case 'it-IT':
                case 'nl':
                case 'nl-NL':
                    return Timezone::findOne(['identifier' => 'Europe/Paris']);

                case 'es-MX':
                    return Timezone::findOne(['identifier' => 'America/Chicago']);

                case 'fr-CA':
                    return Timezone::findOne(['identifier' => 'America/Halifax']);

                case 'ru':
                case 'ru-RU':
                    return Timezone::findOne(['identifier' => 'Europe/Moscow']);

                default:
                    return Timezone::findOne(['identifier' => 'Asia/Tokyo']);
            }
        })();
        if ($tz) {
            Yii::$app->setTimeZone($tz->identifier);
            Yii::$app->setSplatoonRegion($tz->region_id);
        }
    }
}

{'fraction_non_alphanumeric': 0.1596264244709108, 'fraction_numerical': 0.0030845685888098704, 'mean_word_length': 1.3503826016915021, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 128, 'https://': 4, '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': '4865777', 'n_tokens_mistral': 3306, 'n_tokens_neox': 3100, 'n_words': 618}