LocaleMiddleware.php
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace App\Http\Middleware;
use Closure;
use Carbon\Carbon;
/**
* Class LocaleMiddleware.
*/
class LocaleMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
/*
* Locale is enabled and allowed to be changed
*/
if (config('locale.status')) {
if (session()->has('locale') && in_array(session()->get('locale'), array_keys(config('locale.languages')))) {
/*
* Set the Laravel locale
*/
app()->setLocale(session()->get('locale'));
/*
* setLocale for php. Enables ->formatLocalized() with localized values for dates
*/
setlocale(LC_TIME, config('locale.languages')[session()->get('locale')][1]);
/*
* setLocale to use Carbon source locales. Enables diffForHumans() localized
*/
Carbon::setLocale(config('locale.languages')[session()->get('locale')][0]);
/*
* Set the session variable for whether or not the app is using RTL support
* for the current language being selected
* For use in the blade directive in BladeServiceProvider
*/
if (config('locale.languages')[session()->get('locale')][2]) {
session(['lang-rtl' => true]);
} else {
session()->forget('lang-rtl');
}
}
}
return $next($request);
}
}