2fox/yii2-language-picker

There is no license information available for the latest version (dev-master) of this package.

Yii2 Language Picker Widget

dev-master 2018-04-06 08:12 UTC

This package is not auto-updated.

Last update: 2024-04-17 21:15:26 UTC


README

Yii2 Language Picker Widget (FORK FROM https://github.com/lajax/yii2-language-picker)

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.

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).

Installation

The preferred way to install this extension is through composer.

add

"require": {
        "twofox/yii2-language-picker": "*"
},
"repositories": [{
        "type": "git",
        "url": "https://twofox:@bitbucket.org/twofox/yii2-language-picker.git"
}],

to the require section of your composer.json file.

Config:

Identifier of the language element. e.g.: en, en-US

Minimal configuration (icons only)

'language' => 'en',
'bootstrap' => ['languagepicker'],
'components' => [
    'languagepicker' => [
        'class' => 'twofox\languagepicker\Component',
        'languages' => ['en', 'de', 'fr']                   // List of available languages (icons only)
    ]
],
'on beforeAction' => function ($event) {
    \Yii::$app->languagepicker->initLanguage();
}

Minimal configuration (icons and text)

'language' => 'en',
'bootstrap' => ['languagepicker'],
'components' => [
    'languagepicker' => [
        'class' => 'twofox\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' => 'twofox\languagepicker\Component',
        'languages' => ['en-US', 'de-DE', 'fr-FR'],         // List of available languages (icons only)
        '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' => 'twofox\languagepicker\Component',
        'languages' => function () {                        // List of available languages (icons only)
            return array_keys(\lajax\translatemanager\models\Language::getLanguageNames(true));
        }
    ]
],

Full configuration (icons and text)

'language' => 'en-US',
'bootstrap' => ['languagepicker'],
'components' => [
    'languagepicker' => [
        'class' => 'twofox\languagepicker\Component',
        'languages' => function () {                        // List of available languages (icons and text)
            return \lajax\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.

example:

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

Using of urlManager.

Usage

Displaying language selector

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

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

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

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

Customising the language picker:

<?= \twofox\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' => 'twofox\languagepicker\bundles\LanguageLargeIconsAsset',      // StyleSheets
    'languagePluginAsset' => 'twofox\languagepicker\bundles\LanguagePluginAsset',    // JavaScripts
]); ?>