PHPackages                             yidas/yii2-language - 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. [Framework](/categories/framework)
4. /
5. yidas/yii2-language

ActiveYii2-extension[Framework](/categories/framework)

yidas/yii2-language
===================

Yii 2 Framework Language extension with Status Keep and Mapping

1.1.0(7y ago)3692MITPHP

Since Apr 24Pushed 7y ago1 watchersCompare

[ Source](https://github.com/yidas/yii2-language)[ Packagist](https://packagist.org/packages/yidas/yii2-language)[ RSS](/packages/yidas-yii2-language/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

 [ ![](https://avatars0.githubusercontent.com/u/993323) ](https://github.com/yiisoft)

Yii 2 Language Extension
========================

[](#yii-2-language-extension)

Yii 2 Framework Language extension with Status Keep and Mapping

[![Latest Stable Version](https://camo.githubusercontent.com/534a8c8351d5c03959bd59dcd625343727e258a0407073fe3e81a990bb79db6f/68747470733a2f2f706f7365722e707567782e6f72672f79696461732f796969322d6c616e67756167652f762f737461626c653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/yidas/yii2-language)[![Latest Unstable Version](https://camo.githubusercontent.com/bda74dfdb3a4ada2d390e0c0e4e5413bdf5d07a16e8640e953519482b51ed972/68747470733a2f2f706f7365722e707567782e6f72672f79696461732f796969322d6c616e67756167652f762f756e737461626c653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/yidas/yii2-language)[![License](https://camo.githubusercontent.com/7cd7d5988d048c902578c6a24bdcdf77f050a7eb5e2cc9ae15fea9f398867100/68747470733a2f2f706f7365722e707567782e6f72672f79696461732f796969322d6c616e67756167652f6c6963656e73653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/yidas/yii2-language)

FEATURES
--------

[](#features)

- ***language Mapping** integrated with Yii2 Language*
- ***Session &amp; Cookie** storage support*
- ***Yii2 i18n** support*

---

OUTLINE
-------

[](#outline)

- [Requirements](#requirements)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
    - [get()](#get)
    - [set()](#set)
    - [getbymap()](#getbymap)
    - [setbymap()](#setbymap)
    - [isFirstCome()](#isfirstcome)
- [Implementation](implementation)
    - [Controller for Changing Language](#controller-for-changing-language)
    - [BeforeAction for globally changing language](#beforeaction-for-globally-changing-language)

---

REQUIREMENTS
------------

[](#requirements)

This library requires the following:

- PHP 5.4.0+
- Yii 2.0.0+

---

INSTALLATION
------------

[](#installation)

Install via Composer in your Yii2 project:

```
composer require yidas/yii2-language

```

---

CONFIGURATION
-------------

[](#configuration)

Add a component using `yidas\components\Language` with configurations:

```
return [
    'bootstrap' => ['log', 'lang'],
    'language' => 'en-US',
    'components' => [
        'lang' => [
            'class' => 'yidas\components\Language',
            'languages' => [
                0 => 'en-US',
                1 => 'zh-TW',
                2 => 'zh-CN',
            ],
            'maps' => [
                'html' => [
                    0 => 'en',
                    1 => 'zh-Hant',
                    2 => 'zh-Hans',
                ],
            ],
            // 'storage' => 'session',
            // 'storageKey' => 'language',
        ],
        ...
```

propertyTypeDefaultDescriptionlanguagesarrayAs exampleSupported language listmapsarrayAs exampleCustomized language mapstoragestring'session'Storage carrier: 'session' or 'cookie'storageKeystring'language'Storage carrier Key### Bootstrap

[](#bootstrap)

You could add the language component into `bootstrap` for keeping the language storage work such as Seesion and Cookie.

```
// `lang` component for example
return [
    'bootstrap' => ['lang'],
    ...
```

---

USAGE
-----

[](#usage)

### get()

[](#get)

Get Current Language

```
public string get($map=null)
```

*Example:*

```
echo \Yii::$app->lang->get();  // en-US
```

You could get from map by giving map key as first argument:

```
echo \Yii::$app->lang->get('html');  // en
```

### set()

[](#set)

Set Current Language synchronised to `\Yii::$app->language`

```
public boolean set($language)
```

*Example:*

```
\Yii::$app->lang->set('zh-TW');
```

### getByMap()

[](#getbymap)

Get customized language value from $map

```
public string getByMap($mapKey)
```

*Example:*

If you have to echo HTML language value by current language:

```
echo \Yii::$app->lang->getByMap('html');  // en
```

### setByMap()

[](#setbymap)

Set by using customized language value from $map

```
public boolean setByMap($mapKey, $mapValue)
```

*Example:*

If you have to set current language by inputting a HTML language value:

```
$this->setByMap('html', 'zh-Hant');
```

### isFirstCome()

[](#isfirstcome)

First time coming check, which has no StorageRecord

Inverse alias with `hasStorageRecord()`

```
public boolean isFirstCome()
```

*Example:*

```
if (Yii::$app->lang->isFirstCome()) {
    // Detetmine user ip to set current language
}
else if (Yii::$app->lang->hasStorageRecord()) {
    // Means !(Yii::$app->lang->isFirstCome())
}
```

---

IMPLEMENTATION
--------------

[](#implementation)

### Controller for Changing Language

[](#controller-for-changing-language)

You could add a controller or action for changing language like `/language?language=zh-TW`:

```
