@@ -256,23 +256,23 @@ This will give you:
256
256
257
257
When persistence is enabled, the component will fire a ` languageChanged ` event
258
258
whenever the language stored in session or cookie changes. Here's an example
259
- how this can be used:
259
+ how this can be used to track user languages in the database :
260
260
261
261
``` php
262
262
<?php
263
263
264
264
'urlManager' => [
265
265
'class' => 'codemix\localeurls\UrlManager',
266
266
'languages' => ['en', 'fr', 'de'],
267
- 'on languageChanged' => `\app\helpers\MyHelper::languageChanged ',
267
+ 'on languageChanged' => `\app\components\User::onLanguageChanged ',
268
268
]
269
269
```
270
270
271
- The static class method in ` MyHelper ` could look like this:
271
+ The static class method in ` User ` could look like this:
272
272
273
273
``` php
274
274
<?php
275
- public static function languageChanged ($event)
275
+ public static function onLanguageChanged ($event)
276
276
{
277
277
// $event->language: new language
278
278
// $event->oldLanguage: old language
@@ -285,28 +285,8 @@ public static function languageChanged($event)
285
285
}
286
286
}
287
287
```
288
-
289
- You could then for example restore the user language in the ` afterLogin ` event
290
- of a custom ` user ` component:
291
-
292
- ``` php
293
- <?php
294
- public function afterLogin($identity, $cookieBased, $duration)
295
- {
296
- parent::afterLogin($identity, $cookieBased, $duration);
297
- $language = $identity->language;
298
- if ($language !==null && Yii::$app->language !== $language) {
299
- Yii::$app
300
- ->getResponse()
301
- ->redirect(['site/index', 'language' => $language]);
302
- Yii::$app->end();
303
- }
304
- }
305
- ```
306
-
307
- > ** Note:** A language may already have been selected before a new user signs
308
- > up. So remember to also save the app language in the user model when
309
- > inserting a new user.
288
+ > ** Note:** A language may already have been selected before a user logs in or
289
+ > signs up. So you should also save or update the language in these cases.
310
290
311
291
312
292
### Language Detection
0 commit comments