-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5618f24
commit 780230a
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Longman\LaravelLodash\Middlewares; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
|
||
use function app; | ||
use function array_keys; | ||
use function config; | ||
use function setlocale; | ||
|
||
use const LC_ALL; | ||
|
||
class ApiLocale | ||
{ | ||
public function handle(Request $request, Closure $next) | ||
{ | ||
if ($request->wantsJson()) { | ||
$locales = config('lodash.locales', []); | ||
$locale = $request->getPreferredLanguage(array_keys($locales)); | ||
app()->setLocale($locale); | ||
} | ||
|
||
/** @var \Symfony\Component\HttpFoundation\Response $response */ | ||
$response = $next($request); | ||
$locale = app()->getLocale(); | ||
$response->headers->set('Content-Language', $locale, true); | ||
|
||
if (! empty($locales[$locale])) { | ||
setlocale(LC_ALL, $locales[$locale]['full_locale']); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters