2015-08-15 10:00:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
|
|
|
* (c) Alt Three Services Limited
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace CachetHQ\Cachet\Http\Middleware;
|
|
|
|
|
2017-01-03 14:33:22 +00:00
|
|
|
use CachetHQ\Cachet\Settings\Repository as SettingsRepository;
|
2015-08-15 10:00:50 +01:00
|
|
|
use Closure;
|
2017-01-03 14:33:22 +00:00
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
2016-02-11 11:35:40 +00:00
|
|
|
use Illuminate\Http\Request;
|
2017-03-09 19:42:35 +00:00
|
|
|
use Jenssegers\Date\Date;
|
2015-08-15 10:00:50 +01:00
|
|
|
|
2016-10-08 10:37:21 +01:00
|
|
|
/**
|
|
|
|
* This is the localize middleware class.
|
|
|
|
*
|
|
|
|
* @author James Brooks <james@alt-three.com>
|
|
|
|
* @author Joseph Cohen <joe@alt-three.com>
|
|
|
|
* @author Graham Campbell <james@alt-three.com>
|
|
|
|
*/
|
2015-08-15 10:00:50 +01:00
|
|
|
class Localize
|
|
|
|
{
|
|
|
|
/**
|
2017-01-03 14:33:22 +00:00
|
|
|
* The config repository instance.
|
2015-08-15 10:00:50 +01:00
|
|
|
*
|
2017-01-03 14:33:22 +00:00
|
|
|
* @var \Illuminate\Contracts\Config\Repository
|
2015-08-15 10:00:50 +01:00
|
|
|
*/
|
2017-01-03 14:33:22 +00:00
|
|
|
protected $config;
|
2015-08-15 10:00:50 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-03 14:33:22 +00:00
|
|
|
* The settings repository instance.
|
2015-08-15 10:00:50 +01:00
|
|
|
*
|
2017-01-03 14:33:22 +00:00
|
|
|
* @var \CachetHQ\Cachet\Settings\Repository
|
2015-08-15 10:00:50 +01:00
|
|
|
*/
|
2017-01-03 14:33:22 +00:00
|
|
|
protected $settings;
|
2015-08-15 10:00:50 +01:00
|
|
|
|
|
|
|
/**
|
2015-12-24 17:30:59 +00:00
|
|
|
* Constructs a new localize middleware instance.
|
2015-08-15 10:00:50 +01:00
|
|
|
*
|
2017-01-03 14:33:22 +00:00
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config
|
|
|
|
* @param \CachetHQ\Cachet\Settings\Repository $settings
|
2015-09-19 12:30:38 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2015-08-15 10:00:50 +01:00
|
|
|
*/
|
2017-01-03 14:33:22 +00:00
|
|
|
public function __construct(ConfigRepository $config, SettingsRepository $settings)
|
2015-08-15 10:00:50 +01:00
|
|
|
{
|
|
|
|
$this->config = $config;
|
2017-01-03 14:33:22 +00:00
|
|
|
$this->settings = $settings;
|
2015-08-15 10:00:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-02-11 11:35:40 +00:00
|
|
|
public function handle(Request $request, Closure $next)
|
2015-08-15 10:00:50 +01:00
|
|
|
{
|
2017-01-03 14:33:22 +00:00
|
|
|
if (!(bool) $this->settings->get('automatic_localization')) {
|
2016-05-05 10:41:42 +01:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2017-01-03 14:33:22 +00:00
|
|
|
$requestedLanguages = $request->getLanguages();
|
2015-08-15 10:00:50 +01:00
|
|
|
$userLanguage = $this->config->get('app.locale');
|
2017-01-03 14:33:22 +00:00
|
|
|
$langs = $this->config->get('langs');
|
2015-08-15 10:00:50 +01:00
|
|
|
|
2017-01-03 14:33:22 +00:00
|
|
|
foreach ($requestedLanguages as $language) {
|
2015-11-16 23:56:07 +08:00
|
|
|
$language = str_replace('_', '-', $language);
|
2015-08-15 10:00:50 +01:00
|
|
|
|
2017-01-03 14:33:22 +00:00
|
|
|
if (isset($langs[$language])) {
|
2015-08-15 10:00:50 +01:00
|
|
|
$userLanguage = $language;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
app('translator')->setLocale($userLanguage);
|
2017-03-09 19:42:35 +00:00
|
|
|
Date::setLocale($userLanguage);
|
2015-08-15 10:00:50 +01:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|