Skip to content

Commit 3d7e12e

Browse files
committed
Update README and add trace on change event
1 parent 1b8d4e4 commit 3d7e12e

File tree

2 files changed

+7
-26
lines changed

2 files changed

+7
-26
lines changed

README.md

+6-26
Original file line numberDiff line numberDiff line change
@@ -256,23 +256,23 @@ This will give you:
256256

257257
When persistence is enabled, the component will fire a `languageChanged` event
258258
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:
260260

261261
```php
262262
<?php
263263

264264
'urlManager' => [
265265
'class' => 'codemix\localeurls\UrlManager',
266266
'languages' => ['en', 'fr', 'de'],
267-
'on languageChanged' => `\app\helpers\MyHelper::languageChanged',
267+
'on languageChanged' => `\app\components\User::onLanguageChanged',
268268
]
269269
```
270270

271-
The static class method in `MyHelper` could look like this:
271+
The static class method in `User` could look like this:
272272

273273
```php
274274
<?php
275-
public static function languageChanged($event)
275+
public static function onLanguageChanged($event)
276276
{
277277
// $event->language: new language
278278
// $event->oldLanguage: old language
@@ -285,28 +285,8 @@ public static function languageChanged($event)
285285
}
286286
}
287287
```
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.
310290
311291

312292
### Language Detection

UrlManager.php

+1
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ protected function persistLanguage($language)
411411
if ($this->hasEventHandlers(self::EVENT_LANGUAGE_CHANGED)) {
412412
$oldLanguage = $this->loadPersistedLanguage();
413413
if ($oldLanguage !== $language) {
414+
Yii::trace("Triggering languageChanged event: $oldLanguage -> $language", __METHOD__);
414415
$this->trigger(self::EVENT_LANGUAGE_CHANGED, new LanguageChangedEvent([
415416
'oldLanguage' => $oldLanguage,
416417
'language' => $language,

0 commit comments

Comments
 (0)