PHPackages                             gugglegum/yii2-cookie-language-selector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. gugglegum/yii2-cookie-language-selector

ActiveYii2-extension[Localization &amp; i18n](/categories/localization)

gugglegum/yii2-cookie-language-selector
=======================================

Yii2 language selector component based on cookies

1.0.0(10y ago)16503MITPHPPHP &gt;=5.3.0

Since Jul 3Pushed 10y ago2 watchersCompare

[ Source](https://github.com/gugglegum/yii2-cookie-language-selector)[ Packagist](https://packagist.org/packages/gugglegum/yii2-cookie-language-selector)[ RSS](/packages/gugglegum-yii2-cookie-language-selector/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Yii2 Cookie Language Selector
=============================

[](#yii2-cookie-language-selector)

This is very simple component which adds language switch functionality to your site using cookies mechanism. It's not a widget, it doesn't offers any GUI like [lajax/yii2-language-picker](https://github.com/lajax/yii2-language-picker). This may be useful because every real web-site has your own design and HTML markup. Some of web-sites uses ``, some uses `` dropdown list with `onclick` event handler which submits hidden POST form. With this component you're free to use any language selection method. You just need a something able to send POST when needed to switch language and add very simple handler of this POST request.

Installation
------------

[](#installation)

Installation is very easy and contains following 5 steps.

### 1. Install component itself

[](#1-install-component-itself)

Execute following command in directory of your Yii2 application:

```
composer require gugglegum/yii2-cookie-language-selector

```

(you may need to use `composer.phar` instead of `composer`). This will install the `gugglegum/yii2-cookie-language-selector` component into your application.

### 2. Add component to your config

[](#2-add-component-to-your-config)

Go to your frontend's config and add component config into `components` section:

```
        'cookieLanguageSelector' => [
            'class' => 'gugglegum\Yii2\Extension\CookieLanguageSelector\Component',
            'defaultLanguage' => 'en-US',
            'validLanguages' => ['en-US', 'ru-RU', 'de-DE'],
        ],

```

The `defaultLanguage` is language used by default before language selection. The `validLanguages` is the list of languages which can be selected. Additionally you may define name of cookie to store language (`cookieName`) and it's expiration period (`cookieExpire`). By default cookie name is "lang" and it's expires after 7776000 seconds (90 days).

### 3. Add component in bootstrap

[](#3-add-component-in-bootstrap)

Your application stores current language in `Yii::$app->language`. So you need to add this component to `bootstrap` section of your frontend's config to init this component at every application start. You should get something like this:

```
    'bootstrap' => ['log', 'cookieLanguageSelector'],

```

The initialization of `cookieLanguageSelector` will just set `Yii::$app->language` to language retrieved from component. Please use long language format like "en-US", not just "en".

### 4. Create POST form on your web-site

[](#4-create-post-form-on-your-web-site)

You should create HTML form with POST method or ajax with POST to send new language to the server. Your form may contain only `language` parameter. Additionally you may add `redirectTo` parameter with URL your user should be redirected after switching language (usually the same URL where user was before). The names of these parameters (`language` and `redirectTo`) is not predefined, it's completely up to you and they are used only in you handler action described in step 5.

Please avoid use GET method of HTTP for switching language. As described in RFC-2616 the GET method should not be used in case different from retrieving information. Your browser may prefetch links on your page and switch language randomly. On the other hand request with GET method can be cached on proxy, so switch may not work. Please always use POST method for action which doing something.

Here is very simple example of language switching form. Add this to some view or layout:

```

        Current language is

```

Of course you may use more customized variant. For example, you may use CSS stylized dropdown list which submits hidden POST form.

### 5. Create handler action

[](#5-create-handler-action)

Add to your `SiteController.php` following action-method:

```
    /**
     * Switches the language and redirects back
     */
    public function actionSwitchLanguage()
    {
        Yii::$app->cookieLanguageSelector->setLanguage(Yii::$app->request->post('language'));
        $this->redirect(Yii::$app->request->post('redirectTo', ['site/index']));
    }

```

That's all! Your comments and suggestions are welcome.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3972d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8c7fbb933ba217a413e632183e6061f81bd1d285bb86efa82289977038577d88?d=identicon)[gugglegum](/maintainers/gugglegum)

---

Top Contributors

[![gugglegum](https://avatars.githubusercontent.com/u/1580712?v=4)](https://github.com/gugglegum "gugglegum (7 commits)")

---

Tags

i18nyii2extensionmultilanguage

### Embed Badge

![Health badge](/badges/gugglegum-yii2-cookie-language-selector/health.svg)

```
[![Health](https://phpackages.com/badges/gugglegum-yii2-cookie-language-selector/health.svg)](https://phpackages.com/packages/gugglegum-yii2-cookie-language-selector)
```

###  Alternatives

[lajax/yii2-language-picker

Yii2 Language Picker Widget

41230.8k3](/packages/lajax-yii2-language-picker)[geertw/yii2-translatable-url-rule

A custom URL rule class for Yii 2 which allows to create translated URL rules

1420.3k](/packages/geertw-yii2-translatable-url-rule)[yeesoft/yii2-multilingual

Yii2 Multilingual Module

177.4k1](/packages/yeesoft-yii2-multilingual)[devgroup/yii2-multilingual

Allows building yii2 apps for multiple languages using regional URL's and domains

152.6k2](/packages/devgroup-yii2-multilingual)[tigrov/yii2-country

Country data for Yii2 using Intl extension and more.

151.1k](/packages/tigrov-yii2-country)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
