2021-06-26 23:23:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Middleware;
|
2017-01-16 00:27:24 +08:00
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Translation\LanguageManager;
|
2017-01-16 00:27:24 +08:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Closure;
|
|
|
|
|
|
|
|
class Localization
|
|
|
|
{
|
2022-09-03 02:19:01 +08:00
|
|
|
protected LanguageManager $languageManager;
|
2018-09-22 20:18:26 +08:00
|
|
|
|
2022-09-03 02:19:01 +08:00
|
|
|
public function __construct(LanguageManager $languageManager)
|
|
|
|
{
|
|
|
|
$this->languageManager = $languageManager;
|
|
|
|
}
|
2019-01-19 20:11:18 +08:00
|
|
|
|
2017-01-16 00:27:24 +08:00
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2021-06-26 23:23:15 +08:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
*
|
2017-01-16 00:27:24 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
2022-09-03 02:19:01 +08:00
|
|
|
// Get and record the default language in the config
|
2017-01-16 00:27:24 +08:00
|
|
|
$defaultLang = config('app.locale');
|
2019-03-08 05:09:23 +08:00
|
|
|
config()->set('app.default_locale', $defaultLang);
|
2018-08-12 20:10:55 +08:00
|
|
|
|
2022-09-03 02:19:01 +08: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-12 03:35:31 +08:00
|
|
|
|
2018-09-22 20:18:26 +08:00
|
|
|
// Set text direction
|
2022-09-03 02:19:01 +08:00
|
|
|
if ($this->languageManager->isRTL($userLang)) {
|
2018-09-22 20:18:26 +08:00
|
|
|
config()->set('app.rtl', true);
|
|
|
|
}
|
|
|
|
|
2022-09-03 02:19:01 +08:00
|
|
|
app()->setLocale($userLang);
|
|
|
|
Carbon::setLocale($userLang);
|
|
|
|
$this->languageManager->setPhpDateTimeLocale($userLang);
|
2021-06-26 23:23:15 +08:00
|
|
|
|
2017-01-16 00:27:24 +08:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|