Commit 83df6f21 authored by ZeMKI's avatar ZeMKI
Browse files

Update Language.php

* added locale detection and switch to preferred language.
parent 72659ae3
......@@ -3,6 +3,7 @@
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Session\Session;
class Language
{
......@@ -13,12 +14,27 @@ class Language
* @param \Closure $next
* @return mixed
*/
const SESSION_KEY = 'locale';
const LOCALES = ['en', 'de','da'];
public function handle($request, Closure $next)
{
if ($request->session()->exists('locale')) {
\App::setLocale(\Session::get('locale'));
/** @var Session $session */
$session = $request->getSession();
if (!$session->has(self::SESSION_KEY)) {
$session->put(self::SESSION_KEY, $request->getPreferredLanguage(self::LOCALES));
}
if ($request->has('lang')) {
$lang = $request->get('lang');
if (in_array($lang, self::LOCALES)) {
$session->put(self::SESSION_KEY, $lang);
}
}
app()->setLocale($session->get(self::SESSION_KEY));
return $next($request);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment