0

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 Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Route;

$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 | App\Http\Controllers\HomeController@index | web        |
|        |          |                |      |                                           | auth       |
|        | GET|HEAD | en/routes.home | home | App\Http\Controllers\HomeController@index | web        |
|        |          |                |      |                                           | auth       |
+--------+----------+----------------+------+-------------------------------------------+------------+

REFERENCES:

My localization middleware:

<?php

namespace App\Http\Middleware;

use App;
use Closure;

    class Localization
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $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' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \App\Http\Middleware\Localization::class,
    ],

    'api' => [
        'throttle:60,1',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    'localization' => \App\Http\Middleware\Localization::class,
];

Lang folder: Lang folder

wanderlusted
  • 187
  • 13

2 Answers2

0

Make sure you are using Locale middleware

\App\Http\Middleware\Locale::class
b00sted 'snail'
  • 354
  • 1
  • 13
  • 27
  • Thanks, can you please elaborate on how to do that exactly? What should I change in my code? – wanderlusted Sep 30 '20 at 09:17
  • App\Http\Kernel.php $middlewareGroups add that middleware or go to routes.php and add middleware for specific route https://laravel.com/docs/8.x/middleware#assigning-middleware-to-routes – b00sted 'snail' Sep 30 '20 at 09:18
  • Thanks! I did have the localization middleware and now assigned that middleware to the route group (post updated accordingly). Still the routes listed by route:list are not translated. My feeling is that the translation of the route names (Lang:get) is not working properly and also causes this strange side effect of dumping my dictionary files. Let me know, if you have any thoughts on that. Thanks! – wanderlusted Sep 30 '20 at 09:30
  • do you have all these languages in lang folder? – b00sted 'snail' Sep 30 '20 at 09:50
  • Shoot me: I was missing the starting php tag in the language file. Now it is working as expected. – wanderlusted Sep 30 '20 at 11:42
0

I missed the starting php tag in the translation files :-(((. Adding that the translation works fine.

wanderlusted
  • 187
  • 13