66
77/**
88 * Component.
9- *
9+ *
1010 * Examples:
11- *
11+ *
1212 * Minimal code:
13- *
13+ *
1414 * ~~~
1515 * 'language' => 'en',
1616 * 'bootstrap' => ['languagepicker'],
2121 * ]
2222 * ],
2323 * ~~~
24- *
24+ *
2525 * Complete example:
26- *
26+ *
2727 * ~~~
2828 * 'language' => 'en-US',
2929 * 'bootstrap' => ['languagepicker'],
4444 * ]
4545 * ]
4646 * ~~~
47- *
47+ *
4848 *
4949 * @author Lajos Molnar <[email protected] > 50+ *
5051 * @since 1.0
5152 */
5253class Component extends \yii \base \Component
5354{
54-
5555 /**
5656 * @var function - function to execute after changing the language of the site.
5757 */
5858 public $ callback ;
5959
6060 /**
61- * @var integer expiration date of the cookie storing the language of the site.
61+ * @var int expiration date of the cookie storing the language of the site.
6262 */
6363 public $ expireDays = 30 ;
6464
@@ -76,28 +76,28 @@ class Component extends \yii\base\Component
7676 /**
7777 * @var array List of available languages
7878 * Formats supported in the pre-defined skins:
79- *
79+ *
8080 * ~~~
8181 * ['en', 'de', 'es']
8282 * ['en' => 'English', 'de' => 'Deutsch', 'fr' => 'Français']
8383 * ['en-US', 'de-DE', 'fr-FR']
8484 * ['en-US' => 'English', 'de-DE' => 'Deutsch', 'fr-FR' => 'Français']
8585 * ~~~
86- *
8786 */
8887 public $ languages ;
8988
9089 /**
9190 * @inheritdoc
91+ *
9292 * @param array $config
93+ *
9394 * @throws \yii\base\InvalidConfigException
9495 */
95- public function __construct ($ config = array () )
96+ public function __construct ($ config = [] )
9697 {
97-
9898 if (empty ($ config ['languages ' ])) {
9999 throw new \yii \base \InvalidConfigException ('Missing languages ' );
100- } else if (is_callable ($ config ['languages ' ])) {
100+ } elseif (is_callable ($ config ['languages ' ])) {
101101 $ config ['languages ' ] = call_user_func ($ config ['languages ' ]);
102102 }
103103
@@ -109,7 +109,6 @@ public function __construct($config = array())
109109 */
110110 public function init ()
111111 {
112-
113112 $ this ->initLanguage ();
114113
115114 parent ::init ();
@@ -123,12 +122,13 @@ public function initLanguage()
123122 if (isset ($ _GET ['language-picker-language ' ])) {
124123 if ($ this ->_isValidLanguage ($ _GET ['language-picker-language ' ])) {
125124 return $ this ->saveLanguage ($ _GET ['language-picker-language ' ]);
126- } else if (!Yii::$ app ->request ->isAjax ) {
125+ } elseif (!Yii::$ app ->request ->isAjax ) {
127126 return $ this ->_redirect ();
128127 }
129- } else if (Yii::$ app ->request ->cookies ->has ($ this ->cookieName )) {
128+ } elseif (Yii::$ app ->request ->cookies ->has ($ this ->cookieName )) {
130129 if ($ this ->_isValidLanguage (Yii::$ app ->request ->cookies ->getValue ($ this ->cookieName ))) {
131130 Yii::$ app ->language = Yii::$ app ->request ->cookies ->getValue ($ this ->cookieName );
131+
132132 return ;
133133 } else {
134134 Yii::$ app ->response ->cookies ->remove ($ this ->cookieName );
@@ -140,12 +140,13 @@ public function initLanguage()
140140
141141 /**
142142 * Saving language into cookie and database.
143+ *
143144 * @param string $language - The language to save.
145+ *
144146 * @return static
145147 */
146148 public function saveLanguage ($ language )
147149 {
148-
149150 Yii::$ app ->language = $ language ;
150151 $ this ->saveLanguageIntoCookie ($ language );
151152
@@ -170,6 +171,7 @@ public function detectLanguage()
170171 if ($ this ->_isValidLanguage ($ language )) {
171172 Yii::$ app ->language = $ language ;
172173 $ this ->saveLanguageIntoCookie ($ language );
174+
173175 return ;
174176 }
175177 }
@@ -180,6 +182,7 @@ public function detectLanguage()
180182 if (preg_match ('/^ ' . $ pattern . '/ ' , $ value ) || preg_match ('/^ ' . $ pattern . '/ ' , $ key )) {
181183 Yii::$ app ->language = $ this ->_isValidLanguage ($ key ) ? $ key : $ value ;
182184 $ this ->saveLanguageIntoCookie (Yii::$ app ->language );
185+
183186 return ;
184187 }
185188 }
@@ -188,6 +191,7 @@ public function detectLanguage()
188191
189192 /**
190193 * Save language into cookie.
194+ *
191195 * @param string $language
192196 */
193197 public function saveLanguageIntoCookie ($ language )
@@ -196,30 +200,33 @@ public function saveLanguageIntoCookie($language)
196200 'name ' => $ this ->cookieName ,
197201 'domain ' => $ this ->cookieDomain ,
198202 'value ' => $ language ,
199- 'expire ' => time () + 86400 * $ this ->expireDays
203+ 'expire ' => time () + 86400 * $ this ->expireDays ,
200204 ]);
201205
202206 Yii::$ app ->response ->cookies ->add ($ cookie );
203207 }
204208
205209 /**
206210 * Redirects the browser to the referer URL.
211+ *
207212 * @return static
208213 */
209214 private function _redirect ()
210215 {
211216 $ redirect = Yii::$ app ->request ->absoluteUrl == Yii::$ app ->request ->referrer ? '/ ' : Yii::$ app ->request ->referrer ;
217+
212218 return Yii::$ app ->response ->redirect ($ redirect );
213219 }
214220
215221 /**
216222 * Determines whether the language received as a parameter can be processed.
223+ *
217224 * @param string $language
218- * @return boolean
225+ *
226+ * @return bool
219227 */
220228 private function _isValidLanguage ($ language )
221229 {
222230 return is_string ($ language ) && (isset ($ this ->languages [$ language ]) || in_array ($ language , $ this ->languages ));
223231 }
224-
225- }
232+ }
0 commit comments