]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/Localization.php
Played around with a new app structure
[bookstack] / app / Http / Middleware / Localization.php
1 <?php
2
3 namespace BookStack\Http\Middleware;
4
5 use BookStack\Translation\LanguageManager;
6 use Carbon\Carbon;
7 use Closure;
8
9 class Localization
10 {
11     protected LanguageManager $languageManager;
12
13     public function __construct(LanguageManager $languageManager)
14     {
15         $this->languageManager = $languageManager;
16     }
17
18     /**
19      * Handle an incoming request.
20      *
21      * @param \Illuminate\Http\Request $request
22      * @param \Closure                 $next
23      *
24      * @return mixed
25      */
26     public function handle($request, Closure $next)
27     {
28         // Get and record the default language in the config
29         $defaultLang = config('app.locale');
30         config()->set('app.default_locale', $defaultLang);
31
32         // Get the user's language and record that in the config for use in views
33         $userLang = $this->languageManager->getUserLanguage($request, $defaultLang);
34         config()->set('app.lang', str_replace('_', '-', $this->languageManager->getIsoName($userLang)));
35
36         // Set text direction
37         if ($this->languageManager->isRTL($userLang)) {
38             config()->set('app.rtl', true);
39         }
40
41         app()->setLocale($userLang);
42         Carbon::setLocale($userLang);
43         $this->languageManager->setPhpDateTimeLocale($userLang);
44
45         return $next($request);
46     }
47 }