I work on a multi-lang webapp and therefore I also need to handle URLs translated to different languages (for SEO reasons). I’m following the guidelines from this article (second answer), but got stuck with a basic problem. Please see below my web.php:
<?php
use IlluminateSupportFacadesLang;
use IlluminateSupportFacadesRoute;
$all_langs = config('app.all_langs');
//Iterate over each language prefix
foreach( $all_langs as $prefix ){
Route::group(['prefix' => $prefix , 'middleware' => 'localization'], function() use ($prefix) {
if ($prefix == '') $prefix = 'en';
Route::get(Lang::get('routes.home',[], $prefix), 'HomeController@index')->name('home');
});
}
I though that the above code snippet will generate a route that is based on the german translation of ‘home’: for example de/hause (for testing purposes only).
Instead of this the route generated has the URI routes.home, feeling like that the translation did not work. Additionally my translation files (lang/en/routes.php and lang/de/routes.php) are printed out at the top of the page (why is this happening?). Please see the generated route:list illustrating this below:
Ors-MacBook-Pro:gng_backo Ors$ php artisan route:list
return array(
'home' => 'home',
'tours' => 'tours',
'guides' => 'guides'
'login' => 'login'
);return array(
'home' => 'hause',
'tours' => 'touren',
'guides' => 'stadtfuehrer'
);+--------+----------+----------------+------+-------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+----------------+------+-------------------------------------------+------------+
| | GET|HEAD | api/user | | Closure | api |
| | | | | | auth:api |
| | GET|HEAD | de/routes.home | home | AppHttpControllersHomeController@index | web |
| | | | | | auth |
| | GET|HEAD | en/routes.home | home | AppHttpControllersHomeController@index | web |
| | | | | | auth |
+--------+----------+----------------+------+-------------------------------------------+------------+
REFERENCES:
My localization middleware:
<?php
namespace AppHttpMiddleware;
use App;
use Closure;
class Localization
{
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
//setting of the locale based on the first segment of the URL
app()->setLocale($request->segment(1));
return $next($request);
}
}
My kernel:
protected $middlewareGroups = [
'web' => [
AppHttpMiddlewareEncryptCookies::class,
IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
IlluminateSessionMiddlewareStartSession::class,
// IlluminateSessionMiddlewareAuthenticateSession::class,
IlluminateViewMiddlewareShareErrorsFromSession::class,
AppHttpMiddlewareVerifyCsrfToken::class,
IlluminateRoutingMiddlewareSubstituteBindings::class,
AppHttpMiddlewareLocalization::class,
],
'api' => [
'throttle:60,1',
IlluminateRoutingMiddlewareSubstituteBindings::class,
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => AppHttpMiddlewareAuthenticate::class,
'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class,
'cache.headers' => IlluminateHttpMiddlewareSetCacheHeaders::class,
'can' => IlluminateAuthMiddlewareAuthorize::class,
'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,
'password.confirm' => IlluminateAuthMiddlewareRequirePassword::class,
'signed' => IlluminateRoutingMiddlewareValidateSignature::class,
'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,
'verified' => IlluminateAuthMiddlewareEnsureEmailIsVerified::class,
'localization' =>Â AppHttpMiddlewareLocalization::class,
];
2
Answers
I missed the starting php tag in the translation files :-(((. Adding that the translation works fine.
Make sure you are using Locale middleware