PHP code example of cetver / yii2-language-selector

1. Go to this page and download the library: Download cetver/yii2-language-selector library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cetver / yii2-language-selector example snippets


 Yii::$app->language = 'en';
 $languageItem = new cetver\LanguageSelector\items\DropDownLanguageItem([
     'languages' => [
         'en' => '<span class="flag-icon flag-icon-us"></span> English',
         'ru' => '<span class="flag-icon flag-icon-ru"></span> Russian',
         'de' => '<span class="flag-icon flag-icon-de"></span> Deutsch',
     ],
     'options' => ['encode' => false],
 ]);
 \yii\bootstrap\NavBar::begin([
     'brandLabel' => 'My Company',
     'brandUrl' => Yii::$app->homeUrl,
 ]);
 echo \yii\bootstrap\Nav::widget([
     'options' => ['class' => 'navbar-nav navbar-right'],
     'items' => [
         ['label' => 'Home', 'url' => ['/site/index']],
         ['label' => 'About', 'url' => ['/site/about']],
         $languageItem->toArray()
     ]
 ]);
 \yii\bootstrap\NavBar::end();

 Yii::$app->language = 'en';
 $languageItem = new cetver\LanguageSelector\items\DropDownLanguageItem([
     'languages' => [
         'en' => '<span class="flag-icon flag-icon-us"></span> English',
         'ru' => '<span class="flag-icon flag-icon-ru"></span> Russian',
         'de' => '<span class="flag-icon flag-icon-de"></span> Deutsch',
     ],
     'options' => ['encode' => false],
 ]);
 $languageItem = $languageItem->toArray();
 $languageDropdownItems = \yii\helpers\ArrayHelper::remove($languageItem, 'items');
 echo \yii\bootstrap\ButtonDropdown::widget([
     'label' => $languageItem['label'],
     'encodeLabel' => false,
     'options' => ['class' => 'btn-default'],
     'dropdown' => [
         'items' => $languageDropdownItems
     ]
 ]);

$languageItems = new cetver\LanguageSelector\items\MenuLanguageItems([
     'languages' => [
         'en' => '<span class="flag-icon flag-icon-us"></span> English',
         'ru' => '<span class="flag-icon flag-icon-ru"></span> Russian',
         'de' => '<span class="flag-icon flag-icon-de"></span> Deutsch',
     ],
     'options' => ['encode' => false],
 ]);
 echo \yii\widgets\Menu::widget([
     'options' => ['class' => 'list-inline'],
     'items' => $languageItems->toArray(),
 ]);

new cetver\LanguageSelector\items\DropDownLanguageItem([
    // the list of available languages
    'languages' => [
        'en' => 'English',
        'ru' => 'Russian',
        'de' => 'Deutsch',
    ],
    /*
    or
    'languages' => function () {
       return \app\models\Language::find()
                    ->select(['code', 'name'])
                    ->createCommand()
                    ->queryAll(\PDO::FETCH_KEY_PAIR);
    },
    */
    // the drop-down widget item options, excluding "label" and "url"
    // @see \yii\bootstrap\Nav::$items
    // @see \yii\bootstrap\Dropdown::$items
    'options' => [
        'visible' => true
    ]
]);

new cetver\LanguageSelector\items\MenuLanguageItems([
    // the list of available languages
    'languages' => [
        'en' => 'English',
        'ru' => 'Russian',
        'de' => 'Deutsch',
    ],
    /*
    or
    'languages' => function () {
       return \app\models\Language::find()
                    ->select(['code', 'name'])
                    ->createCommand()
                    ->queryAll(\PDO::FETCH_KEY_PAIR);
    },
    */
    // the menu widget item options, excluding "label", "url" and "active"
    // @see \yii\widgets\Menu::$items
    'options' => [
        'visible' => true
    ]
]);