Skip to content

Commit 91aca44

Browse files
vanderbMarc Cámara
authored and
Marc Cámara
committed
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
1 parent f1dbc41 commit 91aca44

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ class Kernel extends HttpKernel {
118118
/**** OTHER MIDDLEWARE ****/
119119
'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
120120
'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
121-
'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class
121+
'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
122+
'localeViewPath' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class
122123
// REDIRECTION MIDDLEWARE
123124
];
124125
}
@@ -131,7 +132,7 @@ class Kernel extends HttpKernel {
131132
Route::group(
132133
[
133134
'prefix' => LaravelLocalization::setLocale(),
134-
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect' ]
135+
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
135136
],
136137
function()
137138
{
@@ -156,6 +157,15 @@ If you want to hide the default locale but always show other locales in the url,
156157

157158
**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.
158159

160+
### Set current locale as view-base-path
161+
162+
To set the current locale as view-base-path, simply register the localeViewPath-middlware in your Kernel.php, like it is descriped above.
163+
164+
Now you can wrap your views in language-based folders like the translation files.
165+
166+
`resources/views/en/`, `resources/vies/fr`, ...
167+
168+
159169
## Helpers
160170

161171
This package comes with some useful functions, like:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Support\Facades\View;
7+
use Illuminate\Http\Request;
8+
9+
class LaravelLocalizationViewPath extends LaravelLocalizationMiddlewareBase
10+
{
11+
/**
12+
* Handle an incoming request.
13+
*
14+
* @param \Illuminate\Http\Request $request
15+
* @param \Closure $next
16+
*
17+
* @return mixed
18+
*/
19+
public function handle($request, Closure $next) {
20+
21+
// If the URL of the request is in exceptions.
22+
if ($this->shouldIgnore($request)) {
23+
return $next($request);
24+
}
25+
26+
$app = app();
27+
28+
$currentLocale = app('laravellocalization')->getCurrentLocale();
29+
$viewPath = resource_path('views/' . $currentLocale);
30+
31+
// Add current locale-code to view-paths
32+
View::addLocation($viewPath);
33+
34+
return $next($request);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)