Skip to content

Commit c2da093

Browse files
authored
Add user detector (#7)
1 parent 4021298 commit c2da093

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

config/localizer.php

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
'detectors' => [
1515
CodeZero\Localizer\Detectors\UrlDetector::class,
16+
CodeZero\Localizer\Detectors\UserDetector::class,
1617
CodeZero\Localizer\Detectors\SessionDetector::class,
1718
CodeZero\Localizer\Detectors\CookieDetector::class,
1819
CodeZero\Localizer\Detectors\BrowserDetector::class,
@@ -34,6 +35,12 @@
3435
*/
3536
'url-segment' => 1,
3637

38+
/**
39+
* The attribute on the user model that holds the locale,
40+
* when using the UserDetector.
41+
*/
42+
'user-attribute' => 'locale',
43+
3744
/**
3845
* The session key that holds the locale,
3946
* when using the SessionDetector and SessionStore.

src/Detectors/UserDetector.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace CodeZero\Localizer\Detectors;
4+
5+
use Illuminate\Support\Facades\Auth;
6+
use Illuminate\Support\Facades\Config;
7+
8+
class UserDetector implements Detector
9+
{
10+
/**
11+
* Detect the locale.
12+
*
13+
* @return string|array|null
14+
*/
15+
public function detect()
16+
{
17+
$user = Auth::user();
18+
19+
if ($user === null) {
20+
return null;
21+
}
22+
23+
$attribute = Config::get('localizer.user-attribute');
24+
25+
return $user->getAttributeValue($attribute);
26+
}
27+
}

0 commit comments

Comments
 (0)