Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

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);
}

}