https://github.com/yidas/yii2-language
Yii 2 Framework Language extension with Status Keep and Mapping
https://github.com/yidas/yii2-language
language language-maps yii2 yii2-extension
Last synced: about 1 year ago
JSON representation
Yii 2 Framework Language extension with Status Keep and Mapping
- Host: GitHub
- URL: https://github.com/yidas/yii2-language
- Owner: yidas
- License: mit
- Created: 2018-04-21T08:13:50.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-01T07:02:29.000Z (over 7 years ago)
- Last Synced: 2025-04-01T02:48:25.370Z (over 1 year ago)
- Topics: language, language-maps, yii2, yii2-extension
- Language: PHP
- Homepage:
- Size: 11.7 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Yii 2 Language Extension
Yii 2 Framework Language extension with Status Keep and Mapping
[](https://packagist.org/packages/yidas/yii2-language)
[](https://packagist.org/packages/yidas/yii2-language)
[](https://packagist.org/packages/yidas/yii2-language)
FEATURES
--------
- ***language Mapping** integrated with Yii2 Language*
- ***Session & Cookie** storage support*
- ***Yii2 i18n** support*
---
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
------------
This library requires the following:
- PHP 5.4.0+
- Yii 2.0.0+
---
INSTALLATION
------------
Install via Composer in your Yii2 project:
```
composer require yidas/yii2-language
```
---
CONFIGURATION
-------------
Add a component using `yidas\components\Language` with configurations:
```php
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',
],
...
```
|property|Type|Default|Description|
|:-|:-|:-|:-|
|languages|array|As example|Supported language list|
|maps|array|As example|Customized language map|
|storage|string|'session'|Storage carrier: 'session' or 'cookie'|
|storageKey|string|'language'|Storage carrier Key|
### Bootstrap
You could add the language component into `bootstrap` for keeping the language storage work such as Seesion and Cookie.
```php
// `lang` component for example
return [
'bootstrap' => ['lang'],
...
```
---
USAGE
-----
### get()
Get Current Language
```php
public string get($map=null)
```
*Example:*
```php
echo \Yii::$app->lang->get(); // en-US
```
You could get from map by giving map key as first argument:
```php
echo \Yii::$app->lang->get('html'); // en
```
### set()
Set Current Language synchronised to `\Yii::$app->language`
```php
public boolean set($language)
```
*Example:*
```php
\Yii::$app->lang->set('zh-TW');
```
### getByMap()
Get customized language value from $map
```php
public string getByMap($mapKey)
```
*Example:*
If you have to echo HTML language value by current language:
```php
echo \Yii::$app->lang->getByMap('html'); // en
```
### setByMap()
Set by using customized language value from $map
```php
public boolean setByMap($mapKey, $mapValue)
```
*Example:*
If you have to set current language by inputting a HTML language value:
```php
$this->setByMap('html', 'zh-Hant');
```
### isFirstCome()
First time coming check, which has no StorageRecord
Inverse alias with `hasStorageRecord()`
```php
public boolean isFirstCome()
```
*Example:*
```php
if (Yii::$app->lang->isFirstCome()) {
// Detetmine user ip to set current language
}
else if (Yii::$app->lang->hasStorageRecord()) {
// Means !(Yii::$app->lang->isFirstCome())
}
```
---
IMPLEMENTATION
--------------
### Controller for Changing Language
You could add a controller or action for changing language like `/language?language=zh-TW`:
```php
lang->set($language);
return $this->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);
}
}
```
### BeforeAction for globally changing language
You could globally set language by handling language setting in the bootstrap of application.
For example, get `GET` parameter to set language in `on beforeAction` function:
```php
return [
'on beforeAction' => function ($event) {
// Always fetch language from get-parameter
$lang = \Yii::$app->request->get('lang');
// Set to given language with get-parameter
if ($lang) {
$result = \Yii::$app->lang->set($lang);
}
},
...
]
```
After that, by giving `lang` param from any url like `/post/my-article?lang=zh-TW` would change language.