2017-01-19 04:04:29 +08:00
|
|
|
<?php namespace BookStack\Http\Middleware;
|
2017-01-16 00:27:24 +08:00
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Closure;
|
2018-08-12 20:10:55 +08:00
|
|
|
use Illuminate\Http\Request;
|
2017-01-16 00:27:24 +08:00
|
|
|
|
|
|
|
class Localization
|
|
|
|
{
|
2018-09-22 20:18:26 +08:00
|
|
|
|
|
|
|
protected $rtlLocales = ['ar'];
|
|
|
|
|
2017-01-16 00:27:24 +08:00
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
|
|
|
$defaultLang = config('app.locale');
|
2018-08-12 20:10:55 +08:00
|
|
|
|
|
|
|
if (user()->isDefault() && config('app.auto_detect_locale')) {
|
|
|
|
$locale = $this->autoDetectLocale($request, $defaultLang);
|
2017-04-29 23:47:41 +08:00
|
|
|
} else {
|
|
|
|
$locale = setting()->getUser(user(), 'language', $defaultLang);
|
|
|
|
}
|
2018-08-12 20:10:55 +08:00
|
|
|
|
2018-09-22 20:18:26 +08:00
|
|
|
// Set text direction
|
|
|
|
if (in_array($locale, $this->rtlLocales)) {
|
|
|
|
config()->set('app.rtl', true);
|
|
|
|
}
|
|
|
|
|
2017-01-16 00:27:24 +08:00
|
|
|
app()->setLocale($locale);
|
|
|
|
Carbon::setLocale($locale);
|
|
|
|
return $next($request);
|
|
|
|
}
|
2018-08-12 20:10:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Autodetect the visitors locale by matching locales in their headers
|
|
|
|
* against the locales supported by BookStack.
|
|
|
|
* @param Request $request
|
|
|
|
* @param string $default
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function autoDetectLocale(Request $request, string $default)
|
|
|
|
{
|
|
|
|
$availableLocales = config('app.locales');
|
|
|
|
foreach ($request->getLanguages() as $lang) {
|
|
|
|
if (in_array($lang, $availableLocales)) {
|
|
|
|
return $lang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $default;
|
|
|
|
}
|
2017-01-16 00:27:24 +08:00
|
|
|
}
|