2017-01-18 15:04:29 -05:00
|
|
|
<?php namespace BookStack\Http\Middleware;
|
2017-01-15 11:27:24 -05:00
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Closure;
|
2018-08-12 08:10:55 -04:00
|
|
|
use Illuminate\Http\Request;
|
2017-01-15 11:27:24 -05:00
|
|
|
|
|
|
|
class Localization
|
|
|
|
{
|
2018-09-22 08:18:26 -04:00
|
|
|
|
2019-01-19 07:11:18 -05:00
|
|
|
/**
|
|
|
|
* Array of right-to-left locales
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-09-22 08:18:26 -04:00
|
|
|
protected $rtlLocales = ['ar'];
|
|
|
|
|
2019-01-19 07:11:18 -05:00
|
|
|
/**
|
|
|
|
* Map of BookStack locale names to best-estimate system locale names.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $localeMap = [
|
|
|
|
'ar' => 'ar',
|
2020-01-18 11:07:36 -05:00
|
|
|
'da' => 'da_DK',
|
2019-01-19 07:11:18 -05:00
|
|
|
'de' => 'de_DE',
|
|
|
|
'de_informal' => 'de_DE',
|
|
|
|
'en' => 'en_GB',
|
|
|
|
'es' => 'es_ES',
|
|
|
|
'es_AR' => 'es_AR',
|
|
|
|
'fr' => 'fr_FR',
|
|
|
|
'it' => 'it_IT',
|
|
|
|
'ja' => 'ja',
|
2019-10-18 09:27:41 -04:00
|
|
|
'ko' => 'ko_KR',
|
2019-01-19 07:11:18 -05:00
|
|
|
'nl' => 'nl_NL',
|
|
|
|
'pl' => 'pl_PL',
|
|
|
|
'pt_BR' => 'pt_BR',
|
|
|
|
'ru' => 'ru',
|
|
|
|
'sk' => 'sk_SK',
|
|
|
|
'sv' => 'sv_SE',
|
|
|
|
'uk' => 'uk_UA',
|
|
|
|
'zh_CN' => 'zh_CN',
|
|
|
|
'zh_TW' => 'zh_TW',
|
2019-10-17 09:01:19 -04:00
|
|
|
'tr' => 'tr_TR',
|
2019-01-19 07:11:18 -05:00
|
|
|
];
|
|
|
|
|
2017-01-15 11:27:24 -05: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');
|
2019-03-07 16:09:23 -05:00
|
|
|
config()->set('app.default_locale', $defaultLang);
|
2018-08-12 08:10:55 -04:00
|
|
|
|
|
|
|
if (user()->isDefault() && config('app.auto_detect_locale')) {
|
|
|
|
$locale = $this->autoDetectLocale($request, $defaultLang);
|
2017-04-29 11:47:41 -04:00
|
|
|
} else {
|
|
|
|
$locale = setting()->getUser(user(), 'language', $defaultLang);
|
|
|
|
}
|
2018-08-12 08:10:55 -04:00
|
|
|
|
2019-08-18 13:57:35 -04:00
|
|
|
config()->set('app.lang', str_replace('_', '-', $this->getLocaleIso($locale)));
|
2019-06-11 15:35:31 -04:00
|
|
|
|
2018-09-22 08:18:26 -04:00
|
|
|
// Set text direction
|
|
|
|
if (in_array($locale, $this->rtlLocales)) {
|
|
|
|
config()->set('app.rtl', true);
|
|
|
|
}
|
|
|
|
|
2017-01-15 11:27:24 -05:00
|
|
|
app()->setLocale($locale);
|
|
|
|
Carbon::setLocale($locale);
|
2019-01-19 07:11:18 -05:00
|
|
|
$this->setSystemDateLocale($locale);
|
2017-01-15 11:27:24 -05:00
|
|
|
return $next($request);
|
|
|
|
}
|
2018-08-12 08:10:55 -04: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;
|
|
|
|
}
|
2019-01-19 07:11:18 -05:00
|
|
|
|
2019-06-11 15:35:31 -04:00
|
|
|
/**
|
|
|
|
* Get the ISO version of a BookStack language name
|
|
|
|
* @param string $locale
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLocaleIso(string $locale)
|
|
|
|
{
|
|
|
|
return $this->localeMap[$locale] ?? $locale;
|
|
|
|
}
|
|
|
|
|
2019-01-19 07:11:18 -05:00
|
|
|
/**
|
|
|
|
* Set the system date locale for localized date formatting.
|
|
|
|
* Will try both the standard locale name and the UTF8 variant.
|
|
|
|
* @param string $locale
|
|
|
|
*/
|
|
|
|
protected function setSystemDateLocale(string $locale)
|
|
|
|
{
|
2019-06-11 15:35:31 -04:00
|
|
|
$systemLocale = $this->getLocaleIso($locale);
|
2019-01-19 07:11:18 -05:00
|
|
|
$set = setlocale(LC_TIME, $systemLocale);
|
|
|
|
if ($set === false) {
|
|
|
|
setlocale(LC_TIME, $systemLocale . '.utf8');
|
|
|
|
}
|
|
|
|
}
|
2017-01-15 11:27:24 -05:00
|
|
|
}
|