|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Http\RedirectResponse; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Http\Response; |
|
9
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
|
10
|
|
|
use Closure; |
|
11
|
|
|
|
|
12
|
|
|
use function session; |
|
13
|
|
|
use function setlocale; |
|
14
|
|
|
|
|
15
|
|
|
class SetAppLocale |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param Request $request |
|
19
|
|
|
* |
|
20
|
|
|
* @return string |
|
21
|
|
|
*/ |
|
22
|
|
|
private function getLocale(Request $request): string |
|
23
|
|
|
{ |
|
24
|
|
|
// The Jaxon request processing path is not localized. So we need to save the current |
|
25
|
|
|
// locale in the session, so we can have it when processing the Jaxon ajax requests. |
|
26
|
|
|
if(!$request->routeIs('jaxon')) |
|
27
|
|
|
{ |
|
28
|
|
|
$locale = LaravelLocalization::getCurrentLocale(); |
|
29
|
|
|
session(['jaxonCurrentLocale' => $locale]); |
|
30
|
|
|
return $locale; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
// For Jaxon requests, try to get the current locale from the session. |
|
34
|
|
|
$locale = session('jaxonCurrentLocale', LaravelLocalization::getCurrentLocale()); |
|
35
|
|
|
LaravelLocalization::setLocale($locale); |
|
|
|
|
|
|
36
|
|
|
return $locale; |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Handle an incoming request. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Request $request |
|
43
|
|
|
* @param Closure(Request): (Response|RedirectResponse) $next |
|
44
|
|
|
* |
|
45
|
|
|
* @return Response|RedirectResponse |
|
46
|
|
|
*/ |
|
47
|
|
|
public function handle(Request $request, Closure $next) |
|
48
|
|
|
{ |
|
49
|
|
|
$locale = $this->getLocale($request); |
|
50
|
|
|
|
|
51
|
|
|
// Set the locale for date and time. |
|
52
|
|
|
Carbon::setLocale($locale); |
|
53
|
|
|
setlocale(LC_TIME, $locale); |
|
54
|
|
|
// For comparison of strings with accented characters in french |
|
55
|
|
|
setlocale(LC_COLLATE, LaravelLocalization::getCurrentLocaleRegional() . '.utf8'); |
|
56
|
|
|
|
|
57
|
|
|
return $next($request); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|