Skip to content

Instantly share code, notes, and snippets.

@zoparga
Created April 6, 2021 11:49
Show Gist options
  • Save zoparga/f2dfc5f5b13d5d29de278220c277be48 to your computer and use it in GitHub Desktop.
Save zoparga/f2dfc5f5b13d5d29de278220c277be48 to your computer and use it in GitHub Desktop.
Laravel localize middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class SetLanguage
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$domain = parse_url($request->url(), PHP_URL_HOST);
if ($domain == 'www.asd.hu' || $domain == 'asd.hu') {
$language = 'hu';
} else {
$language = 'en';
}
//View::share('language', $language); // Could be useful in views?
App::setLocale($language);
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment