-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathComponent.php
232 lines (205 loc) · 6.25 KB
/
Component.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
namespace lajax\languagepicker;
use Yii;
/**
* Component.
*
* Examples:
*
* Minimal code:
*
* ~~~
* 'language' => 'en',
* 'bootstrap' => ['languagepicker'],
* 'components' => [
* 'languagepicker' => [
* 'class' => 'lajax\languagepicker\Component',
* 'languages' => ['en', 'de', 'fr'] // List of available languages
* ]
* ],
* ~~~
*
* Complete example:
*
* ~~~
* 'language' => 'en-US',
* 'bootstrap' => ['languagepicker'],
* 'components' => [
* 'languagepicker' => [
* 'class' => 'lajax\languagepicker\Component',
* 'languages' => ['en-US', 'de-DE', 'fr-FR'], // List of available languages
* 'cookieName' => 'language', // Name of the cookie.
* 'cookieDomain' => 'example.com', // Domain of the cookie.
* 'expireDays' => 64, // The expiration time of the cookie is 64 days.
* 'callback' => function() {
* if (!\Yii::$app->user->isGuest) {
* $user = User::findOne(\Yii::$app->user->id);
* $user->language = \Yii::$app->language;
* $user->save();
* }
* }
* ]
* ]
* ~~~
*
*
* @author Lajos Molnar <[email protected]>
*
* @since 1.0
*/
class Component extends \yii\base\Component
{
/**
* @var function - function to execute after changing the language of the site.
*/
public $callback;
/**
* @var int expiration date of the cookie storing the language of the site.
*/
public $expireDays = 30;
/**
* @var string Name of the cookie.
*/
public $cookieName = 'language';
/**
* @var string The domain that the language cookie is available to.
* For details see the $domain parameter description of PHP setcookie() function.
*/
public $cookieDomain = '';
/**
* @var array List of available languages
* Formats supported in the pre-defined skins:
*
* ~~~
* ['en', 'de', 'es']
* ['en' => 'English', 'de' => 'Deutsch', 'fr' => 'Français']
* ['en-US', 'de-DE', 'fr-FR']
* ['en-US' => 'English', 'de-DE' => 'Deutsch', 'fr-FR' => 'Français']
* ~~~
*/
public $languages;
/**
* @inheritdoc
*
* @param array $config
*
* @throws \yii\base\InvalidConfigException
*/
public function __construct($config = [])
{
if (empty($config['languages'])) {
throw new \yii\base\InvalidConfigException('Missing languages');
} elseif (is_callable($config['languages'])) {
$config['languages'] = call_user_func($config['languages']);
}
parent::__construct($config);
}
/**
* @inheritdoc
*/
public function init()
{
$this->initLanguage();
parent::init();
}
/**
* Setting the language of the site.
*/
public function initLanguage()
{
if (isset($_GET['language-picker-language'])) {
if ($this->_isValidLanguage($_GET['language-picker-language'])) {
return $this->saveLanguage($_GET['language-picker-language']);
} elseif (!Yii::$app->request->isAjax) {
return $this->_redirect();
}
} elseif (Yii::$app->request->cookies->has($this->cookieName)) {
if ($this->_isValidLanguage(Yii::$app->request->cookies->getValue($this->cookieName))) {
Yii::$app->language = Yii::$app->request->cookies->getValue($this->cookieName);
return;
} else {
Yii::$app->response->cookies->remove($this->cookieName);
}
}
$this->detectLanguage();
}
/**
* Saving language into cookie and database.
*
* @param string $language - The language to save.
*
* @return static
*/
public function saveLanguage($language)
{
Yii::$app->language = $language;
$this->saveLanguageIntoCookie($language);
if (is_callable($this->callback)) {
call_user_func($this->callback);
}
if (Yii::$app->request->isAjax) {
Yii::$app->end();
}
return $this->_redirect();
}
/**
* Determine language based on UserAgent.
*/
public function detectLanguage()
{
$acceptableLanguages = Yii::$app->getRequest()->getAcceptableLanguages();
foreach ($acceptableLanguages as $language) {
if ($this->_isValidLanguage($language)) {
Yii::$app->language = $language;
$this->saveLanguageIntoCookie($language);
return;
}
}
foreach ($acceptableLanguages as $language) {
$pattern = preg_quote(substr($language, 0, 2), '/');
foreach ($this->languages as $key => $value) {
if (preg_match('/^' . $pattern . '/', $value) || preg_match('/^' . $pattern . '/', $key)) {
Yii::$app->language = $this->_isValidLanguage($key) ? $key : $value;
$this->saveLanguageIntoCookie(Yii::$app->language);
return;
}
}
}
}
/**
* Save language into cookie.
*
* @param string $language
*/
public function saveLanguageIntoCookie($language)
{
$cookie = new \yii\web\Cookie([
'name' => $this->cookieName,
'domain' => $this->cookieDomain,
'value' => $language,
'expire' => time() + 86400 * $this->expireDays,
]);
Yii::$app->response->cookies->add($cookie);
}
/**
* Redirects the browser to the referer URL.
*
* @return static
*/
private function _redirect()
{
$redirect = Yii::$app->request->absoluteUrl == Yii::$app->request->referrer ? '/' : Yii::$app->request->referrer;
return Yii::$app->response->redirect($redirect);
}
/**
* Determines whether the language received as a parameter can be processed.
*
* @param string $language
*
* @return bool
*/
private function _isValidLanguage($language)
{
return is_string($language) && (isset($this->languages[$language]) || in_array($language, $this->languages));
}
}