common/actions/SetLocaleAction.php
<?php
/*
* This file is part of the HomeFinanceV2 project.
*
* (c) Rafalsky.com <http://github.com/Rafalsky/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Author: Eugine Terentev <eugine@terentev.net>
*/
namespace common\actions;
use cheatsheet\Time;
use yii\base\Action;
use yii\base\InvalidParamException;
use yii\web\Cookie;
/**
* Class SetLocaleAction
* @package common\actions
*
* Example:
*
* public function actions()
* {
* return [
* 'set-locale'=>[
* 'class' => 'common\actions\SetLocaleAction',
* 'locales'=>[
* 'en-US', 'ru-RU', 'ua-UA'
* ],
* 'localeCookieName' => '_locale',
* 'callback'=>function($action){
* return $this->controller->redirect(/.. some url ../)
* }
* ]
* ];
* }
*/
class SetLocaleAction extends Action
{
/**
* @var array List of available locales
*/
public $locales;
/**
* @var string
*/
public $localeCookieName = '_locale';
/**
* @var integer
*/
public $cookieExpire;
/**
* @var string
*/
public $cookieDomain;
/**
* @var \Closure
*/
public $callback;
/**
* @param $locale
* @return mixed|static
* @throws \yii\base\InvalidCallException
* @throws \yii\base\InvalidParamException
*/
public function run($locale)
{
if (!is_array($this->locales) || !in_array($locale, $this->locales, true)) {
throw new InvalidParamException('Unacceptable locale');
}
$cookie = new Cookie([
'name' => $this->localeCookieName,
'value' => $locale,
'expire' => $this->cookieExpire ?: time() + Time::SECONDS_IN_A_YEAR,
'domain' => $this->cookieDomain ?: '',
]);
\Yii::$app->getResponse()->getCookies()->add($cookie);
if ($this->callback && $this->callback instanceof \Closure) {
return call_user_func_array($this->callback, [
$this,
$locale
]);
}
return \Yii::$app->response->redirect(\Yii::$app->request->referrer ?: \Yii::$app->homeUrl);
}
}