Skip to content

Instantly share code, notes, and snippets.

@vanderb
Last active February 29, 2020 16:59
Show Gist options
  • Save vanderb/80dea9a8039dd60962c5cb291eae7b75 to your computer and use it in GitHub Desktop.
Save vanderb/80dea9a8039dd60962c5cb291eae7b75 to your computer and use it in GitHub Desktop.
Laravel | language-based view-path Middleware

Laravel | language-based view-path Middleware

Register Middleware in Kernel by adding

  \App\Http\Middleware\LocalizeViewPath::class

to $middlewareGroup array

Now you have to wrap all of your views language based

  • resources/views/en
  • resources/views/fr
  • resources/views/de ...
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\View;
class LocalizeViewPath {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next) {
$locale = app()->getLocale();
$viewPath = resource_path('views/' . $locale);
View::addLocation($viewPath);
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment