Skip to content

Commit

Permalink
Added new middleware to dynamically use current locale as base-view-p…
Browse files Browse the repository at this point in the history
…ath (#446)

* Added new middleware to dynamically use current locale as base-view-path

* Updated Readme
  • Loading branch information
vanderb authored and Marc Cámara committed May 11, 2017
1 parent f1dbc41 commit 91aca44
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}
Expand All @@ -131,7 +132,7 @@ class Kernel extends HttpKernel {
Route::group(
[
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect' ]
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
],
function()
{
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Middleware;

This comment has been minimized.

Copy link
@vasyldutchak

vasyldutchak May 12, 2017

Is it correct path? Maybe it's should be Mcamara\LaravelLocalization\Middleware?


use Closure;
use Illuminate\Support\Facades\View;
use Illuminate\Http\Request;

class LaravelLocalizationViewPath extends LaravelLocalizationMiddlewareBase
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next) {

// If the URL of the request is in exceptions.
if ($this->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);
}

}

0 comments on commit 91aca44

Please sign in to comment.