From 91aca44cf993f3ea14d70cc388062775516d57f5 Mon Sep 17 00:00:00 2001 From: Freerk van Zeijl Date: Thu, 11 May 2017 08:03:31 +0200 Subject: [PATCH] Added new middleware to dynamically use current locale as base-view-path (#446) * Added new middleware to dynamically use current locale as base-view-path * Updated Readme --- README.md | 14 ++++++- .../LaravelLocalizationViewPath.php | 37 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationViewPath.php diff --git a/README.md b/README.md index 5258ae0..9d1f0de 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,8 @@ class Kernel extends HttpKernel { /**** OTHER MIDDLEWARE ****/ 'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class, 'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class, - 'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class + 'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class, + 'localeViewPath' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class // REDIRECTION MIDDLEWARE ]; } @@ -131,7 +132,7 @@ class Kernel extends HttpKernel { Route::group( [ 'prefix' => LaravelLocalization::setLocale(), - 'middleware' => [ 'localeSessionRedirect', 'localizationRedirect' ] + 'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ] ], function() { @@ -156,6 +157,15 @@ If you want to hide the default locale but always show other locales in the url, **IMPORTANT** - When `hideDefaultLocaleInURL` is set to true, the unlocalized root is treated as the applications default locale `app.locale`. Because of this language negotiation using the Accept-Language header will **NEVER** occur when `hideDefaultLocaleInURL` is true. +### Set current locale as view-base-path + +To set the current locale as view-base-path, simply register the localeViewPath-middlware in your Kernel.php, like it is descriped above. + +Now you can wrap your views in language-based folders like the translation files. + +`resources/views/en/`, `resources/vies/fr`, ... + + ## Helpers This package comes with some useful functions, like: diff --git a/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationViewPath.php b/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationViewPath.php new file mode 100644 index 0000000..f51a035 --- /dev/null +++ b/src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationViewPath.php @@ -0,0 +1,37 @@ +shouldIgnore($request)) { + return $next($request); + } + + $app = app(); + + $currentLocale = app('laravellocalization')->getCurrentLocale(); + $viewPath = resource_path('views/' . $currentLocale); + + // Add current locale-code to view-paths + View::addLocation($viewPath); + + return $next($request); + } + +}