|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\I18n; |
|
3
|
|
|
|
|
4
|
|
|
use Cake\Controller\Controller; |
|
5
|
|
|
use Cake\Core\Configure; |
|
6
|
|
|
use Cake\I18n\I18n; |
|
7
|
|
|
|
|
8
|
|
|
class Language |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The Cookie instance. |
|
13
|
|
|
* |
|
14
|
|
|
* @var \Cake\Network\Session |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $_session; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The Cookie instance. |
|
20
|
|
|
* |
|
21
|
|
|
* @var \Cake\Controller\Component\CookieComponent |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $_cookie; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The Controller instance. |
|
27
|
|
|
* |
|
28
|
|
|
* @var \Cake\Controller\Controller |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $_controller; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* All locales allowed to be used. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $_locales = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The locale to be used. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $_locale; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Construct method. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Cake\Controller\Controller $controller The controller that was fired. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(Controller $controller) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->_controller = $controller; |
|
54
|
|
|
$this->_session = $controller->request->session(); |
|
55
|
|
|
$this->_cookie = $controller->Cookie; |
|
56
|
|
|
$this->_locales = Configure::read('I18n.locales'); |
|
|
|
|
|
|
57
|
|
|
$this->_locale = I18n::locale(); |
|
58
|
|
|
|
|
59
|
|
|
$this->_cookie->configKey('language', [ |
|
60
|
|
|
'expires' => '+1 year', |
|
61
|
|
|
'httpOnly' => true |
|
62
|
|
|
]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Set the language for the user. |
|
67
|
|
|
* |
|
68
|
|
|
* @return void |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setLanguage() |
|
71
|
|
|
{ |
|
72
|
|
|
if ($this->_controller->Auth->user()) { |
|
73
|
|
|
//The user has already a valid language defined in the database. |
|
74
|
|
|
if ($this->_session->read('Auth.User.language') && isset($this->_locales[$this->_session->read('Auth.User.language')])) { |
|
|
|
|
|
|
75
|
|
|
//If the user has not the cookie, we set the cookie. |
|
76
|
|
|
if (!$this->_cookie->check('language') || $this->_cookie->read('language') != $this->_session->read('Auth.User.language')) { |
|
77
|
|
|
$this->_cookie->write('language', $this->_session->read('Auth.User.language')); |
|
78
|
|
|
} |
|
79
|
|
|
//Stock the locale of the user. |
|
80
|
|
|
$this->_locale = $this->_session->read('Auth.User.language'); |
|
81
|
|
|
} |
|
82
|
|
|
} else { |
|
83
|
|
|
//The user has a valid cookie. |
|
84
|
|
|
if ($this->_cookie->check('language') && isset($this->_locales[$this->_cookie->read('language')])) { |
|
85
|
|
|
$this->_locale = $this->_cookie->read('language'); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
//The user want to change his language. |
|
90
|
|
|
if (!is_null($this->_controller->request->getParam('lang')) && isset($this->_locales[$this->_controller->request->getParam('lang')])) { |
|
91
|
|
|
//If the user is connected, we need to save the new language in the database and refresh his session. |
|
92
|
|
|
if ($this->_controller->Auth->user()) { |
|
93
|
|
|
$this->_controller->loadModel('Users'); |
|
94
|
|
|
|
|
95
|
|
|
$user = $this->_controller->Users |
|
96
|
|
|
->find() |
|
97
|
|
|
->where(['id' => $this->_session->read('Auth.User.id')]) |
|
98
|
|
|
->first(); |
|
99
|
|
|
|
|
100
|
|
|
$user->language = $this->_controller->request->getParam('lang'); |
|
101
|
|
|
$this->_controller->Users->save($user); |
|
102
|
|
|
$this->_session->write('Auth.User.language', $this->_controller->request->getParam('lang')); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
//Save the new language in the cookie. |
|
106
|
|
|
$this->_cookie->write('language', $this->_controller->request->getParam('lang')); |
|
107
|
|
|
|
|
108
|
|
|
$this->_locale = $this->_controller->request->getParam('lang'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
//Set the locale. |
|
112
|
|
|
I18n::locale($this->_locale); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..