1cb6ae39c8
For #939 - Adds way to check if current language is RTL via config system. - Made TinyMCE default direction be based on current language text direction. - Fixed bullet points to be RTL compatible. - Set page content body to have direction based on content.
56 lines
1.4 KiB
PHP
56 lines
1.4 KiB
PHP
<?php namespace BookStack\Http\Middleware;
|
|
|
|
use Carbon\Carbon;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
|
|
class Localization
|
|
{
|
|
|
|
protected $rtlLocales = ['ar'];
|
|
|
|
/**
|
|
* Handle an incoming request.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \Closure $next
|
|
* @return mixed
|
|
*/
|
|
public function handle($request, Closure $next)
|
|
{
|
|
$defaultLang = config('app.locale');
|
|
|
|
if (user()->isDefault() && config('app.auto_detect_locale')) {
|
|
$locale = $this->autoDetectLocale($request, $defaultLang);
|
|
} else {
|
|
$locale = setting()->getUser(user(), 'language', $defaultLang);
|
|
}
|
|
|
|
// Set text direction
|
|
if (in_array($locale, $this->rtlLocales)) {
|
|
config()->set('app.rtl', true);
|
|
}
|
|
|
|
app()->setLocale($locale);
|
|
Carbon::setLocale($locale);
|
|
return $next($request);
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|