Merge pull request #905 from cachethq/client-localize
Localize the status page to the visitors lang. Closes #835
This commit is contained in:
@@ -45,6 +45,7 @@ class Kernel extends HttpKernel
|
||||
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
|
||||
'csrf' => 'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken',
|
||||
'guest' => 'CachetHQ\Cachet\Http\Middleware\RedirectIfAuthenticated',
|
||||
'localize' => 'CachetHQ\Cachet\Http\Middleware\Localize',
|
||||
'throttling' => 'GrahamCampbell\Throttle\Http\Middleware\ThrottleMiddleware',
|
||||
];
|
||||
}
|
||||
|
||||
70
app/Http/Middleware/Localize.php
Normal file
70
app/Http/Middleware/Localize.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Config\Repository;
|
||||
|
||||
class Localize
|
||||
{
|
||||
/**
|
||||
* Array of languages Cachet can use.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $langs;
|
||||
|
||||
/**
|
||||
* Config repository.
|
||||
*
|
||||
* @var \Illuminate\Config\Repository
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Constructs a new localize instance.
|
||||
*
|
||||
* @param \Illuminate\Config\Repository $config
|
||||
*/
|
||||
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
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
$supportedLanguages = $request->getLanguages();
|
||||
$userLanguage = $this->config->get('app.locale');
|
||||
|
||||
foreach ($supportedLanguages as $language) {
|
||||
$language = substr($language, 0, 2);
|
||||
|
||||
if (isset($this->langs[$language])) {
|
||||
$userLanguage = $language;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
app('translator')->setLocale($userLanguage);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ class StatusPageRoutes
|
||||
public function map(Registrar $router)
|
||||
{
|
||||
$router->get('/', [
|
||||
'middleware' => 'app.hasSetting',
|
||||
'middleware' => ['app.hasSetting', 'localize'],
|
||||
'setting' => 'app_name',
|
||||
'as' => 'status-page',
|
||||
'uses' => 'HomeController@showIndex',
|
||||
|
||||
@@ -28,7 +28,7 @@ class SubscribeRoutes
|
||||
public function map(Registrar $router)
|
||||
{
|
||||
$router->group([
|
||||
'middleware' => 'app.hasSetting',
|
||||
'middleware' => ['app.hasSetting', 'localize'],
|
||||
'setting' => 'app_name',
|
||||
'as' => 'subscribe.',
|
||||
], function ($router) {
|
||||
|
||||
Reference in New Issue
Block a user