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