<commit_before><?php
/*
author :: <NAME>
website :: http://plusmagi.com
change language by get language=EN, language=TH,...
or select on this widget
*/

namespace app\components;

use Yii;
use yii\base\Component;
use yii\base\Widget;
use yii\bootstrap\Nav;
use yii\bootstrap\ButtonDropdown;
use yii\bootstrap\Dropdown;
use yii\helpers\Url;
use yii\web\Cookie;

class languageSwitcher extends Nav
{
    public $type;

    public $languages = [
        'en-GB' => 'English',
        //'en-US'=>'American English',
        'de-DE' => 'German',
        'fr' => 'French',
//        'th' => 'Thai',
    ];

    public function init()
    {
        if (php_sapi_name() === 'cli') {
            return true;
        }

        parent::init();

        $cookies = Yii::$app->response->cookies;
        $languageNew = Yii::$app->request->get('language');
        if ($languageNew) {
            $this->setNewLanguage($languageNew);

            /*
            if (isset($this->languages[$languageNew])) {

                $this->setNewLanguage($languageNew);

                $cookies->add(new \yii\web\Cookie([
                    'name' => 'language',
                    'value' => $languageNew
                ]));
            }*/
        } elseif ($cookies->has('language')) {
            $this->setNewLanguage($cookies->getValue('language'));
        }

    }

    protected function setNewLanguage($languageNew)
    {
        if (isset($this->languages[$languageNew])) {
            if ($languageNew != Yii::$app->sourceLanguage) {
                Yii::$app->language = $languageNew;
                $cookies = Yii::$app->response->cookies;
                $cookies->add(new \yii\web\Cookie([
                    'name' => 'language',
                    'value' => $languageNew
                ]));
            } else {
                Yii::$app->language = Yii::$app->sourceLanguage;
            }
        }
    }

    public function run()
    {
        $languages = $this->languages;


        //print_pre(Yii::$app->language,'default language');

        $current = $languages[Yii::$app->language];
        unset($languages[Yii::$app->language]);

        $items = [];
        foreach ($languages as $code => $language) {
            $temp = [];
            $temp['label'] = $language;
            $temp['url'] = Url::current(['language' => $code]);
            array_push($items, $temp);
        }

        $this->items[] = [
            'label' => $current,
            'items' => $items,
        ];
        echo parent::run();
    }

    /* // old function when the class was extending from Widget and not from Nav
    public function run()
    {
        $languages = $this->languages;<commit_msg>fixed languageSwitcher get language from cookie and removed unnecessary code<commit_after><?php
/*
author :: <NAME>
website :: http://plusmagi.com
change language by get language=EN, language=TH,...
or select on this widget
*/

namespace app\components;

use Yii;
use yii\base\Component;
use yii\base\Widget;
use yii\bootstrap\Nav;
use yii\bootstrap\ButtonDropdown;
use yii\bootstrap\Dropdown;
use yii\helpers\Url;
use yii\web\Cookie;

class languageSwitcher extends Nav
{
    public $type;

    public $languages = [
        'en-GB' => 'English',
        //'en-US'=>'American English',
        'de-DE' => 'German',
        'fr' => 'French',
//        'th' => 'Thai',
    ];

    public function init()
    {
        if (php_sapi_name() === 'cli') {
            return true;
        }

        parent::init();

        $cookies = Yii::$app->request->cookies;

        $languageNew = Yii::$app->request->get('language');
        if ($languageNew) {
            $this->setNewLanguage($languageNew);
        } elseif ($cookies->has('language')) {
            $this->setNewLanguage($cookies->getValue('language'));
        }
    }

    protected function setNewLanguage($languageNew)
    {
        if (isset($this->languages[$languageNew])) {
            if ($languageNew != Yii::$app->sourceLanguage) {
                Yii::$app->language = $languageNew;
                $cookies = Yii::$app->response->cookies;
                $cookies->add(new \yii\web\Cookie([
                    'name' => 'language',
                    'value' => $languageNew
                ]));
            } else {
                Yii::$app->language = Yii::$app->sourceLanguage;
            }
        }
    }

    public function run()
    {
        $languages = $this->languages;
        //print_pre(Yii::$app->language,'default language');

        $current = $languages[Yii::$app->language];
        unset($languages[Yii::$app->language]);

        $items = [];
        foreach ($languages as $code => $language) {
            $temp = [];
            $temp['label'] = $language;
            $temp['url'] = Url::current(['language' => $code]);
            array_push($items, $temp);
        }

        $this->items[] = [
            'label' => $current,
            'items' => $items,
        ];
        echo parent::run();
    }

    /* // old function when the class was extending from Widget and not from Nav
    public function run()
    {
        $languages = $this->languages;
{'fraction_non_alphanumeric': 0.165084285991087, 'fraction_numerical': 0.0, 'mean_word_length': 1.9065315315315314, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 76, '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': '7463231', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1503, 'n_words': 299}