Class lajax\languagepicker\Component

Inheritancelajax\languagepicker\Component » yii\base\Component
Available since version1.0

Component.

Examples:

Minimal code:

'language' => 'en',
'bootstrap' => ['languagepicker'],
'components' => [
     'languagepicker' => [
         'class' => 'lajax\languagepicker\Component',
         'languages' => ['en', 'de', 'fr']               // List of available languages
     ]
],

Complete example:

'language' => 'en-US',
'bootstrap' => ['languagepicker'],
'components' => [
     'languagepicker' => [
         'class' => 'lajax\languagepicker\Component',
         'languages' => ['en-US', 'de-DE', 'fr-FR'],     // List of available languages
         '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 = User::findOne(\Yii::$app->user->id);
                 $user->language = \Yii::$app->language;
                 $user->save();
             }
         }
     ]
]

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$callback \lajax\languagepicker\function - function to execute after changing the language of the site. lajax\languagepicker\Component
$cookieDomain string The domain that the language cookie is available to. lajax\languagepicker\Component
$cookieName string Name of the cookie. lajax\languagepicker\Component
$expireDays integer Expiration date of the cookie storing the language of the site. lajax\languagepicker\Component
$languages array List of available languages Formats supported in the pre-defined skins:
 ['en', 'de', 'es']
 ['en' => 'English', 'de' => 'Deutsch', 'fr' => 'Français']
 ['en-US', 'de-DE', 'fr-FR']
 ['en-US' => 'English', 'de-DE' => 'Deutsch', 'fr-FR' => 'Français']
lajax\languagepicker\Component

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__construct() lajax\languagepicker\Component
detectLanguage() Determine language based on UserAgent. lajax\languagepicker\Component
init() lajax\languagepicker\Component
initLanguage() Setting the language of the site. lajax\languagepicker\Component
saveLanguage() Saving language into cookie and database. lajax\languagepicker\Component
saveLanguageIntoCookie() Save language into cookie. lajax\languagepicker\Component

Property Details

$callback public property
\lajax\languagepicker\function $callback null
  • function to execute after changing the language of the site.
$cookieDomain public property

The domain that the language cookie is available to. For details see the $domain parameter description of PHP setcookie() function.

$cookieName public property
string $cookieName 'language'

Name of the cookie.

$expireDays public property

Expiration date of the cookie storing the language of the site.

$languages public property

List of available languages Formats supported in the pre-defined skins:

 ['en', 'de', 'es']
 ['en' => 'English', 'de' => 'Deutsch', 'fr' => 'Français']
 ['en-US', 'de-DE', 'fr-FR']
 ['en-US' => 'English', 'de-DE' => 'Deutsch', 'fr-FR' => 'Français']

Method Details

__construct() public method

void __construct$config = [] )
$config array
throws \yii\base\InvalidConfigException
detectLanguage() public method

Determine language based on UserAgent.

void detectLanguage( )
init() public method

void init( )
initLanguage() public method

Setting the language of the site.

void initLanguage( )
saveLanguage() public method

Saving language into cookie and database.

static saveLanguage$language )
$language string
  • The language to save.
saveLanguageIntoCookie() public method

Save language into cookie.

void saveLanguageIntoCookie$language )
$language string