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-02 16:11:15 +00:00
|
|
|
use Carbon\Carbon;
|
2015-08-15 10:00:50 +01:00
|
|
|
use Closure;
|
|
|
|
use Illuminate\Config\Repository;
|
2016-02-11 11:35:40 +00:00
|
|
|
use Illuminate\Http\Request;
|
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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Array of languages Cachet can use.
|
|
|
|
*
|
2017-01-02 16:11:15 +00:00
|
|
|
* @var string[]
|
2015-08-15 10:00:50 +01:00
|
|
|
*/
|
|
|
|
protected $langs;
|
|
|
|
|
|
|
|
/**
|
2016-02-02 20:49:00 +00:00
|
|
|
* The config repository instance.
|
2015-08-15 10:00:50 +01:00
|
|
|
*
|
|
|
|
* @var \Illuminate\Config\Repository
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
2015-12-24 17:30:59 +00:00
|
|
|
* Constructs a new localize middleware instance.
|
2015-08-15 10:00:50 +01:00
|
|
|
*
|
|
|
|
* @param \Illuminate\Config\Repository $config
|
2015-09-19 12:30:38 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2015-08-15 10:00:50 +01:00
|
|
|
*/
|
|
|
|
public function __construct(Repository $config)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
$this->langs = $config->get('langs');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2016-05-26 11:35:00 +01:00
|
|
|
if (!(bool) $this->config->get('setting.automatic_localization')) {
|
2016-05-05 10:41:42 +01:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2015-08-15 10:00:50 +01:00
|
|
|
$supportedLanguages = $request->getLanguages();
|
|
|
|
$userLanguage = $this->config->get('app.locale');
|
|
|
|
|
|
|
|
foreach ($supportedLanguages as $language) {
|
2015-11-16 23:56:07 +08:00
|
|
|
$language = str_replace('_', '-', $language);
|
2015-08-15 10:00:50 +01:00
|
|
|
|
|
|
|
if (isset($this->langs[$language])) {
|
|
|
|
$userLanguage = $language;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
app('translator')->setLocale($userLanguage);
|
2017-01-02 16:11:15 +00:00
|
|
|
Carbon::setLocale($userLanguage);
|
2015-08-15 10:00:50 +01:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|