Skip to content

Add user detector #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/localizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
'detectors' => [
CodeZero\Localizer\Detectors\UrlDetector::class,
CodeZero\Localizer\Detectors\UserDetector::class,
CodeZero\Localizer\Detectors\SessionDetector::class,
CodeZero\Localizer\Detectors\CookieDetector::class,
CodeZero\Localizer\Detectors\BrowserDetector::class,
Expand All @@ -34,6 +35,12 @@
*/
'url-segment' => 1,

/**
* The attribute on the user model that holds the locale,
* when using the UserDetector.
*/
'user-attribute' => 'locale',

/**
* The session key that holds the locale,
* when using the SessionDetector and SessionStore.
Expand Down
27 changes: 27 additions & 0 deletions src/Detectors/UserDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace CodeZero\Localizer\Detectors;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;

class UserDetector implements Detector
{
/**
* Detect the locale.
*
* @return string|array|null
*/
public function detect()
{
$user = Auth::user();

if ($user === null) {
return null;
}

$attribute = Config::get('localizer.user-attribute');

return $user->getAttributeValue($attribute);
}
}