From b8ed85ef8d5c4e3b5b417f94df0ce7f0669cf53e Mon Sep 17 00:00:00 2001 From: Freerk van Zeijl Date: Thu, 4 May 2017 17:32:25 +0200 Subject: [PATCH 1/2] Added new middleware to dynamically use current locale as base-view-path --- .../LaravelLocalizationViewPath.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/Mcamara/LaravelLocalization/Middleware/LaravelLocalizationViewPath.php 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); + } + +} From 48d2612e357054e2a1ef5be878bcaf1e768a75da Mon Sep 17 00:00:00 2001 From: Freerk van Zeijl Date: Mon, 8 May 2017 14:20:26 +0200 Subject: [PATCH 2/2] Updated Readme --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e18da61..0bf76ae 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: