-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManagerLanguage.php
196 lines (163 loc) · 4.1 KB
/
ManagerLanguage.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace panix\engine;
use Yii;
use yii\base\Component;
use panix\mod\admin\models\Languages;
/**
* Class ManagerLanguage
* @package panix\engine
*
* @property Languages $default
* @property Languages $active
* @property array $languages
*/
class ManagerLanguage extends Component
{
/**
* @var array available system languages
*/
protected $_languages = [];
/**
* @var string Active lang code
*/
protected $_active;
/**
* @var string Default lang code
*/
protected $_default;
public function init()
{
if (empty($this->_languages)) {
$this->loadLanguages();
}
}
/**
* Load available languages.
* @return array Languages collection
*/
private function loadLanguages()
{
try {
$model = Languages::find()->published()->all();
foreach ($model as $lang) {
/** @var Languages $lang */
$this->_languages[$lang->code] = $lang;
if ($lang->is_default) {
$this->_default = $lang->code;
}
}
} catch (\yii\db\Exception $e) {
}
return $this->_languages;
}
/**
* Get system languages
* @param boolean $published
* @return array
*/
public function getLanguages($published = true)
{
if ($published) {
return $this->_languages;
} else {
return Languages::find()->asArray()->all();
}
}
/**
* Get lang by its code
* @param string $langCode
* @return Languages
*/
public function getByCode($langCode)
{
if (isset($this->_languages[$langCode]))
return $this->_languages[$langCode];
}
/**
* Get language by its id
* @param integer $langId Language id
* @return mixed LanguageModel if lang found. Null if not.
*/
public function getById($langId)
{
foreach ($this->_languages as $lang) {
if ($lang->id == $langId)
return $lang;
}
}
/**
* Get language codes
* @return array ['en','ru',...]
*/
public function getCodes()
{
return array_keys($this->_languages);
}
/**
* Get default system model
* @return Languages
*/
public function getDefault()
{
return $this->getByCode($this->_default);
}
/**
* Get active language model
* @return Languages
*/
public function getActive()
{
return $this->getByCode($this->_active);
}
/**
* @return array
*/
public function getLangs()
{
$langs = array();
foreach ($this->getLanguages() as $lang) {
if ($this->_default == $lang['code']) {
$langs[''] = $lang['name'];
} else {
$langs[$lang['code']] = $lang['name'];
}
}
return $langs;
}
/**
* Activate language by code
* @param string $code Language code.
*/
public function setActive($code = null)
{
$model = $this->getByCode($code);
if (!$model)
$model = $this->default;
if ($model) {
//Yii::$app->setLanguage($model->locale); // locale
Yii::$app->language = $model->code; // locale
$this->_active = $model->code;
}
}
/**
* Get language prefix to create url.
* If current language is default prefix will be empty.
* @return string Url prefix
*/
public function getUrlPrefix()
{
if ($this->_active !== $this->_default)
return $this->_active;
}
/**
* @return array
*/
public function getLangsByArray()
{
$langs = array();
foreach ($this->getLanguages() as $lang) {
$langs[$lang->id] = $lang->name;
}
return $langs;
}
}