yii2 view ajax,GitHub - lajax/yii2-language-picker: Yii2 Language picker widget

Yii2 Language Picker

Yii2 Language Picker Widget

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.

composer require lajax/yii2-language-picker

Config:

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

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

],

// ...

],

// ...

]

Minimal configuration (icons only)

'language' => 'en',

'bootstrap' => ['languagepicker'],

'components' => [

'languagepicker' => [

'class' => 'lajax\languagepicker\Component',

// List of available languages (icons only)

'languages' => ['en', 'de', 'fr'],

]

],

Minimal configuration (icons and text)

'language' => 'en',

'bootstrap' => ['languagepicker'],

'components' => [

'languagepicker' => [

'class' => 'lajax\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' => 'lajax\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.

// Function to execute after changing the language

'callback' => function() {

if (!\Yii::$app->user->isGuest) {

$user = \Yii::$app->user->identity;

$user->language = \Yii::$app->language;

$user->save();

}

}

]

],

Minimal configuration (icons only)

'language' => 'en',

'bootstrap' => ['languagepicker'],

'components' => [

'languagepicker' => [

'class' => 'lajax\languagepicker\Component',

// Get available languages from translate manager

'languages' => function () {

return array_keys(\lajax\translatemanager\models\Language::getLanguageNames(true));

}

]

],

Minimal configuration (icons and text)

'language' => 'en',

'bootstrap' => ['languagepicker'],

'components' => [

'languagepicker' => [

'class' => 'lajax\languagepicker\Component',

// Get available languages from translate manager

'languages' => function () {

return \lajax\translatemanager\models\Language::getLanguageNames(true);

}

]

],

Usage

Displaying language selector

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

= \lajax\languagepicker\widgets\LanguagePicker::widget([

'skin' => \lajax\languagepicker\widgets\LanguagePicker::SKIN_BUTTON,

'size' => \lajax\languagepicker\widgets\LanguagePicker::SIZE_SMALL

]); ?>

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

= \lajax\languagepicker\widgets\LanguagePicker::widget([

'skin' => \lajax\languagepicker\widgets\LanguagePicker::SKIN_DROPDOWN,

'size' => \lajax\languagepicker\widgets\LanguagePicker::SIZE_LARGE

]); ?>

Customising the language picker:

= \lajax\languagepicker\widgets\LanguagePicker::widget([

'itemTemplate' => '

{name}',

'activeItemTemplate' => ' {name}',

'parentTemplate' => '

',

'languageAsset' => 'lajax\languagepicker\bundles\LanguageLargeIconsAsset', // StyleSheets

'languagePluginAsset' => 'lajax\languagepicker\bundles\LanguagePluginAsset', // JavaScripts

]); ?>

Screenshots

Buttons icons and text

485c1e72b58430f583afcdc0d14124c5.png

Buttons icons only

549c0c95f717d96ad74e010a6a017036.png

Buttons text only

b8cefbd9562e12fc3839b19d97bc7095.png

DropDown icons and text

75feb5c647d265076382f000b1831a87.png

DropDown icons only

5f6643a830d48954fd9da201076de57f.png

DropDown text only

38e4b9f569ab3db753fc2da410b7ff81.png

Links

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值