Laravel 5.4多语言应用程序

时间:2018-08-06 21:52:56

标签: laravel-5.4

我遵循this article进行了多语言申请。但是我不明白如何获得此类链接:

domain.com/about
domain.com/fr/about

我的默认语言是En。我不需要默认的语言前缀。在本文中,语言前缀已添加到所有路由。

路线:

Route::get('/', ['as' => 'home', 'uses' => 'PostController@index']);
Route::get('post/{slug}', ['as' => 'post.show', 'uses' => 'PostController@show']);
Route::get(trans('routes.about'), ['as' => 'about', 'uses' => 'PageController@getAboutPage']);
Route::get('lang/{language}', ['as' => 'lang.switch', 'uses' => 'LanguageController@switchLang']);

Auth::routes();
Route::get('logout', 'Auth\LoginController@logout');

中间件:

public function handle(Request $request, Closure $next)
    {
        // Check if the first segment matches a language code
        if (! array_key_exists($request->segment(1), config('translatable.locales'))) {

            // Store segments in array
            $segments = $request->segments();

            // Set the default language code as the first segment
            $segments = array_prepend($segments, config('app.fallback_locale'));

            // Redirect to the correct url
            return redirect()->to(implode('/', $segments));
        }

        return $next($request);
    }

RouteServiceProvider:

protected function mapWebRoutes()
    {
        $locale = Request::segment(1);

        Route::prefix($locale)
             ->middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

0 个答案:

没有答案