-
Notifications
You must be signed in to change notification settings - Fork 4
/
MultiLanguageUrlManager.php
70 lines (58 loc) · 1.34 KB
/
MultiLanguageUrlManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace webvimark\behaviors\multilanguage;
use yii\web\UrlManager;
use Yii;
class MultiLanguageUrlManager extends UrlManager
{
/**
* init
*
* Adding language prefixes to url rules
*/
public function init()
{
$languages = array_keys(Yii::$app->params['mlConfig']['languages']);
if (count($languages) > 0)
{
$langPrefix = '<_language:('.implode('|', $languages).')>/';
$finalRules[$langPrefix] = '/';
foreach ($this->rules as $rule => $path)
{
if ( is_array($path) && isset($path['pattern']) && isset($path[0]) )
{
$finalRules[$langPrefix . ltrim($path['pattern'], '/')] = $path[0];
}
else
{
$finalRules[$langPrefix . ltrim($rule, '/')] = $path;
}
}
$this->rules = array_merge_recursive($finalRules, $this->rules);
}
parent::init();
}
/**
* createUrl
* Adding language parameter to links
*
* @param array $params
*
* @return string
*/
public function createUrl($params)
{
if (!isset($params['_language']))
{
if (Yii::$app->session->has('_language'))
{
Yii::$app->language = Yii::$app->session->get('_language');
}
elseif (Yii::$app->request->cookies->has('_language'))
{
Yii::$app->language = Yii::$app->request->cookies->get('_language');
}
$params['_language'] = Yii::$app->language;
}
return parent::createUrl($params);
}
}