2017 © Pedro PelĂĄez
 

yii2-extension yii2-language-picker

Yii2 Language Picker Widget

image

user1007017/yii2-language-picker

Yii2 Language Picker Widget

  • Tuesday, May 9, 2017
  • by user1007017
  • Repository
  • 0 Watchers
  • 0 Stars
  • 1 Installations
  • CSS
  • 0 Dependents
  • 0 Suggesters
  • 22 Forks
  • 0 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

Yii2 Language Picker

Yii2 Language Picker Widget, (*1)

Introduction

The widget provides an easy to use language selector which makes it possible to change the language of our website easily. The language change can take place synchronously or asynchronously (through Ajax). The default method is asynchronous (through an Ajax call), however when this method fails for any reason (e.g. JavaScript is blocked on the client side) the new language will actualise synchronously through an automatic page reload., (*2)

The language switcher is fully customisable. However, the pre-defined options provide a dropdown list (DropDownList), and a link-based list (ButtonList). Both versions can display the name of the chosen language and the corresponding flag (icon)., (*3)

Installation

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

Either run, (*5)

php composer.phar require --prefer-dist user1007017/yii2-language-picker "1.*"

or add, (*6)

"user1007017/yii2-language-picker": "1.*"

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

Config:

Identifier of the language element. e.g.: en, en-US, (*8)

Minimal configuration (icons only)

'language' => 'en',
'bootstrap' => ['languagepicker'],
'components' => [
    'languagepicker' => [
        'class' => 'user1007017\languagepicker\Component',
        'languages' => ['en', 'de', 'fr']                   // List of available languages (icons only)
    ]
],

Minimal configuration (icons and text)

'language' => 'en',
'bootstrap' => ['languagepicker'],
'components' => [
    'languagepicker' => [
        'class' => 'user1007017\languagepicker\Component',        // List of available languages (icons and text)
        'languages' => ['en' => 'English', 'de' => 'Deutsch', 'fr' => 'Français']
    ]
],

Full configuration (icons only)

'language' => 'en-US',
'bootstrap' => ['languagepicker'],
'components' => [
    'languagepicker' => [
        'class' => 'user1007017\languagepicker\Component',
        'languages' => ['en-US', 'de-DE', 'fr-FR'],         // List of available languages (icons only)
        'cookieName' => 'language',                         // Name of the cookie.
        'cookieDomain' => 'example.com',                    // Domain of the cookie.
        'expireDays' => 64,                                 // The expiration time of the cookie is 64 days.
        'callback' => function() {
            if (!\Yii::$app->user->isGuest) {
                $user = \Yii::$app->user->identity;
                $user->language = \Yii::$app->language;
                $user->save();
            }
        }
    ]
],

Yii2-translate-manager integration

Minimal configuration (icons only)

'language' => 'en',
'bootstrap' => ['languagepicker'],
'components' => [
    'languagepicker' => [
        'class' => 'user1007017\languagepicker\Component',
        'languages' => function () {                        // List of available languages (icons only)
            return array_keys(\user1007017\translatemanager\models\Language::getLanguageNames(true));
        }
    ]
],

Full configuration (icons and text)

'language' => 'en-US',
'bootstrap' => ['languagepicker'],
'components' => [
    'languagepicker' => [
        'class' => 'user1007017\languagepicker\Component',
        'languages' => function () {                        // List of available languages (icons and text)
            return \user1007017\translatemanager\models\Language::getLanguageNames(true);
        },
        'cookieName' => 'language',                         // Name of the cookie.
        'cookieDomain' => 'example.com',                    // Domain of the cookie.
        'expireDays' => 64,                                 // The expiration time of the cookie is 64 days.
        'callback' => function() {
            if (!\Yii::$app->user->isGuest) {
                $user = \Yii::$app->user->identity;
                $user->language = \Yii::$app->language;
                $user->save();
            }
        }
    ]
],

IMPORTANT

To use the widget, the value of the enablePrettyUrl property in the urlManager configuration must be true, and the value of showScriptName false., (*9)

example:, (*10)

'components' => [
    // ...
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            // your rules go here
        ],
        // ...
    ],
    // ...
]

Using of urlManager., (*11)

Usage

Displaying language selector

Displaying pre-defined languate picker buttons (icons and text or icons only):

<?= \user1007017\languagepicker\widgets\LanguagePicker::widget([
    'skin' => \user1007017\languagepicker\widgets\LanguagePicker::SKIN_BUTTON,
    'size' => \user1007017\languagepicker\widgets\LanguagePicker::SIZE_SMALL
]); ?>

Displaying pre-defined languate picker dropdown list (icons and text or icons only):

<?= \user1007017\languagepicker\widgets\LanguagePicker::widget([
    'skin' => \user1007017\languagepicker\widgets\LanguagePicker::SKIN_DROPDOWN,
    'size' => \user1007017\languagepicker\widgets\LanguagePicker::SIZE_LARGE
]); ?>

Customising the language picker:

<?= \user1007017\languagepicker\widgets\LanguagePicker::widget([
    'itemTemplate' => '<li><a href="{link}" title="{language}"><i id="{language}"></i> {name}</a></li>',
    'activeItemTemplate' => '<a href="{link}" title="{language}"><i id="{language}"></i> {name}</a>',
    'parentTemplate' => '<div class="language-picker dropdown-list {size}"><div>{activeItem}<ul>{items}</ul></div></div>',
    'languageAsset' => 'user1007017\languagepicker\bundles\LanguageLargeIconsAsset',      // StyleSheets
    'languagePluginAsset' => 'user1007017\languagepicker\bundles\LanguagePluginAsset',    // JavaScripts
]); ?>

Other Example hiding active Language

NavBar::begin([
    'brandLabel' => Html::img('@web/css/images/logo.png', ['class' => 'img-responsive','alt' => Yii::$app->name]),
    'brandUrl' => Yii::$app->homeUrl,
    'options' => [
        'class' => 'navbar navbar-default navbar-fixed-top',
    ],
]);

$aNavOptionsTop = [
    'options' => ['class' => 'navbar-nav navbar-left'],
//        'options' => ['class' => 'nav nav-justified '],
    'items' => $items
];
echo Nav::widget($aNavOptionsTop);

//---------------------------LANGUAGE PICKER ---------------------------------------------
echo  \user1007017\languagepicker\widgets\LanguagePicker::widget([
    'skin' => \user1007017\languagepicker\widgets\LanguagePicker::SKIN_BUTTON,
    'size' => \user1007017\languagepicker\widgets\LanguagePicker::SIZE_SMALL,
    'itemTemplate' => '<a href="{link}" title="{language}"> {name}</a>',
    //hide active language (display:none):
    'activeItemTemplate' => '<a style="display:none" href="{link}" title="{language}" class="active"></a>',
    // parentTemplate without {size} !!!:
    'parentTemplate' => '<ul style="list-style: none;" class="navbar-nav navbar-right nav"><li>{items}</li></ul>',

]);
//-------------------------------------------------------------------------------
NavBar::end();

Screenshots

Buttons icons and text

language-picker-0 2-screen-1, (*12)

Buttons icons only

language-picker-0 2-screen-2, (*13)

Buttons text only

language-picker-0 2-screen-3, (*14)

language-picker-0 2-screen-4, (*15)

language-picker-0 2-screen-5, (*16)

language-picker-0 2-screen-5, (*17)

The Versions

09/05 2017

dev-master

9999999-dev

Yii2 Language Picker Widget

  Sources   Download

BSD-3-Clause

The Requires

 

by Yoda

extension yii2 widget translate multilanguage sample languagepicker

01/09 2015

1.2.0

1.2.0.0

Yii2 Language Picker Widget

  Sources   Download

BSD-3-Clause

The Requires

 

by Lajos Molnar

extension yii2 widget translate multilanguage sample languagepicker

12/05 2015

1.1.5

1.1.5.0

Yii2 Language Picker Widget

  Sources   Download

BSD-3-Clause

The Requires

 

by Lajos Molnar

extension yii2 widget translate multilanguage sample languagepicker

26/04 2015

1.1.4

1.1.4.0

Yii2 Language Picker Widget

  Sources   Download

BSD-3-Clause

The Requires

 

by Lajos Molnar

extension yii2 widget translate multilanguage sample languagepicker

07/04 2015

1.1.2

1.1.2.0

Yii2 Language Picker Widget

  Sources   Download

BSD-3-Clause

The Requires

 

by Lajos Molnar

extension yii2 widget translate multilanguage sample languagepicker

05/04 2015

1.1.1

1.1.1.0

Yii2 Language Picker Widget

  Sources   Download

BSD-3-Clause

The Requires

 

by Lajos Molnar

extension yii2 widget translate multilanguage sample languagepicker

29/03 2015

1.1.0

1.1.0.0

Yii2 Language Picker Widget

  Sources   Download

BSD-3-Clause

The Requires

 

by Lajos Molnar

extension yii2 widget translate multilanguage sample languagepicker

25/03 2015

1.0.1

1.0.1.0

Yii2 Language Picker Widget

  Sources   Download

BSD-3-Clause

The Requires

 

by Lajos Molnar

extension yii2 widget translate multilanguage sample languagepicker

01/03 2015

1.0.0

1.0.0.0

Yii2 Language Picker Widget

  Sources   Download

BSD-3-Clause

The Requires

 

by Lajos Molnar

extension yii2 widget translate multilanguage sample languagepicker