以下は、静的テキストの多言語機能を実現するために行った作業です。Laravel 5.0、多言語ウェブサイト
ステップ1:config/app.phpを開き、以下の 'locale' => 'xx'を追加します。
'locales' => ['en' => 'English', 'sv' => 'Swedish'],
ステップ2:私たちのルートをプレフィックスにあなたのルート
を接頭辞は、私たちは、アプリ/プロバイダ/ RouteServiceProvider.phpにマップ方法を変更します。次のマップ方法を変更します。
public function map(Router $router, Request $request)
{
$locale = $request->segment(1);
$this->app->setLocale($locale);
$router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) {
require app_path('Http/routes.php');
});
}
次に、ファイルの先頭に以下を追加:
use Illuminate\Routing\Router;
use Illuminate\Http\Request;
ステップ3:今、言語という名前のファイルを作成し、言語ミドルウェア
を作成します。このコンテンツのアプリ/ Http /ミドルウェアのPHP:
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Routing\Redirector;
use Illuminate\Http\Request;
use Illuminate\Foundation\Application;
use Illuminate\Contracts\Routing\Middleware;
class Language implements Middleware {
public function __construct(Application $app, Redirector $redirector, Request $request) {
$this->app = $app;
$this->redirector = $redirector;
$this->request = $request;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// Make sure current locale exists.
$locale = $request->segment(1);
if (! array_key_exists($locale, $this->app->config->get('app.locales'))) {
$segments = $request->segments();
$segments[0] = $this->app->config->get('app.fallback_locale');
return $this->redirector->to(implode('/', $segments));
}
$this->app->setLocale($locale);
return $next($request);
}
}
ここで、そのミドルウェアをapp/Http/Kernel.phpの$ middlewareプロパティに追加することで、リクエストを処理します。アレイの最上部に追加することをお勧めします。
protected $middleware = [
'App\Http\Middleware\Language',
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession',
'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken',
];
上記のすべての上記のすべて行った後here
から撮影された、私のルートが自動的に言語を選択していない... 私のルートは
Route::get('/', '[email protected]');
Route::get('/about', '[email protected]');
であると私は与える場合ページ内の完全なパス http://localhost:8000/pl/about
これは機能します。
しかし、自動的にパスを選択する必要があることは、ページに完全なURLを指定する必要がないことを意味します。
これはどのように達成できますか?
私はいくつかの点でroutes.phpで作業する必要があることを知っていますが、動作させる方法はわかりません。
私は私の最初のプロジェクトに取り組んでいますように私はLaravel
でLaravelでは非常に限られた知識を持っている
実際には、リクエスト自体からaccept言語ヘッダーを取るべきです。あなたはそれについてもっと読むことができますhttps://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 –
私はそれを理解するとは思わない...私は何をすべきか?私はこの受け入れ言語のヘッダを置くべきです – rgoyal
また、フォーム提出で、私はRouteCollection.php行145で 'NotFoundHttpExceptionエラーを受け取ります:' – rgoyal