2021-06-26 17:23:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Middleware;
|
2017-01-15 17:27:24 +01:00
|
|
|
|
2022-09-02 20:19:01 +02:00
|
|
|
use BookStack\Util\LanguageManager;
|
2017-01-15 17:27:24 +01:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Closure;
|
|
|
|
|
|
|
|
class Localization
|
|
|
|
{
|
2022-09-02 20:19:01 +02:00
|
|
|
protected LanguageManager $languageManager;
|
2018-09-22 14:18:26 +02:00
|
|
|
|
2022-09-02 20:19:01 +02:00
|
|
|
public function __construct(LanguageManager $languageManager)
|
|
|
|
{
|
|
|
|
$this->languageManager = $languageManager;
|
|
|
|
}
|
2019-01-19 13:11:18 +01:00
|
|
|
|
2017-01-15 17:27:24 +01:00
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2021-06-26 17:23:15 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
*
|
2017-01-15 17:27:24 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
2022-09-02 20:19:01 +02:00
|
|
|
// Get and record the default language in the config
|
2017-01-15 17:27:24 +01:00
|
|
|
$defaultLang = config('app.locale');
|
2019-03-07 22:09:23 +01:00
|
|
|
config()->set('app.default_locale', $defaultLang);
|
2018-08-12 14:10:55 +02:00
|
|
|
|
2022-09-02 20:19:01 +02:00
|
|
|
// Get the user's language and record that in the config for use in views
|
|
|
|
$userLang = $this->languageManager->getUserLanguage($request, $defaultLang);
|
|
|
|
config()->set('app.lang', str_replace('_', '-', $this->languageManager->getIsoName($userLang)));
|
2019-06-11 21:35:31 +02:00
|
|
|
|
2018-09-22 14:18:26 +02:00
|
|
|
// Set text direction
|
2022-09-02 20:19:01 +02:00
|
|
|
if ($this->languageManager->isRTL($userLang)) {
|
2018-09-22 14:18:26 +02:00
|
|
|
config()->set('app.rtl', true);
|
|
|
|
}
|
|
|
|
|
2022-09-02 20:19:01 +02:00
|
|
|
app()->setLocale($userLang);
|
|
|
|
Carbon::setLocale($userLang);
|
|
|
|
$this->languageManager->setPhpDateTimeLocale($userLang);
|
2021-06-26 17:23:15 +02:00
|
|
|
|
2017-01-15 17:27:24 +01:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|