2

I am using trans (Lang::get) on routes like this:

...
Route::get(trans('url.news'), 'NewsController@index');
...

And it has working well for fixed language websites.

Now I am switching the language in middleware based on the domains .tld

// app/Http/Middleware/Localization.php
public function handle($request, Closure $next)
{
    $domain = substr(Request::server('HTTP_HOST'),strpos(Request::server('HTTP_HOST'), '.')+1);
    case "domain.com":
        App::setLocale('en');
        \Session::put('connection', 'db_en');
        break;
    case "domain.fr":
        App::setLocale('fr');
        \Session::put('connection', 'db_fr');
        break;
    }
    return $next($request);
}

I have url.php files with the route translations:

// resources/lang/en/url.php
return [
    ....
    'products' => 'products'
    .... 
];

// resources/lang/fr/url.php
return [
    ....
    'products' => 'nos-produits'
    ....
];

Everything is working well except that the middleware seems to be processed AFTER the routes (and the default language files are loaded instead of the one chosen in the middleware). Can I do something to fix this?

For example, is it Ok to do it in the AppServiceProvider.php?

4

0 回答 0