2017 © Pedro Peláez
 

yii2-extension yii2-language-selector

Provides the configuration for the language selector for the Yii framework 2.0

image

cetver/yii2-language-selector

Provides the configuration for the language selector for the Yii framework 2.0

  • Saturday, January 13, 2018
  • by cetver
  • Repository
  • 1 Watchers
  • 3 Stars
  • 1,140 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 36 % Grown

The README.md

Language Selector

Build Status Coverage Status, (*1)

Provides the configuration for the language selector, (*2)

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

composer require --prefer-dist cetver/yii2-language-selector

or add, (*5)

"cetver/yii2-language-selector": "^1.0"

to the require section of your composer.json file., (*6)

Usage

Bootstrap Nav

 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();

Bootstrap Nav Screenshot, (*7)

Bootstrap Drop-Down Button

 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
     ]
 ]);

Bootstrap Drop-Down Button Screenshot, (*8)

$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(),
 ]);

Menu, (*9)

Configuration

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
    ]
]);

Tests

Run the following commands, (*10)

composer create-project --prefer-source cetver/yii2-language-selector
cd yii2-language-selector
vendor/bin/codecept run unit

For I18N support, take a look at

The Versions

13/01 2018

dev-master

9999999-dev

Provides the configuration for the language selector for the Yii framework 2.0

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexandr Cetvertacov

language url extension yii2 switch select locale i18n internationalization toggle change pick

13/01 2018

1.0.3

1.0.3.0

Provides the configuration for the language selector for the Yii framework 2.0

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexandr Cetvertacov

language url extension yii2 switch select locale i18n internationalization toggle change pick

08/01 2018

1.0.2

1.0.2.0

Provides the configuration for the language selector for the Yii framework 2.0

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexandr Cetvertacov

language url extension yii2 switch select locale i18n internationalization toggle change pick

08/01 2018

1.0.1

1.0.1.0

Provides the configuration for the language selector for the Yii framework 2.0

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexandr Cetvertacov

language url extension yii2 switch select locale i18n internationalization toggle change pick

21/07 2017

1.0.0

1.0.0.0

Provides the configuration for the language selector for the Yii framework 2.0

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Alexandr Cetvertacov

language url extension yii2 switch select locale i18n internationalization toggle change pick