Laravel 5多语言支持

时间:2015-03-26 13:40:56

标签: php laravel-5 multilingual laravel-routing

我是laravel的新手,我正在尝试用多语言来支持我的应用程序。 我已经为laravel 5下载了默认的Login System,并从下面的链接中集成了多支持语言教程

https://laracasts.com/discuss/channels/tips/example-on-how-to-use-multiple-locales-in-your-laravel-5-website

但它为我提供了login page url localhost/en/login多语言支持教程,但是当我使用login system而没有集成多语言支持时,它为我提供了登录页面laravel/auth/login的网址。

这是我的kernel.php

class Kernel extends HttpKernel {

    /**
     * The application's global HTTP middleware stack.
     *
     * @var array
     */
    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',
        'App\Http\Middleware\VerifyCsrfToken',
    ];

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'test'=> 'App\Http\Middleware\Test',
        'auth' => 'App\Http\Middleware\Authenticate',
        'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
        'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
    ];

}

这是我的RouteServiceProvider.php

<?php namespace App\Providers;

use Illuminate\Routing\Router;
use Illuminate\Http\Request;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider {

    /**
     * This namespace is applied to the controller routes in your routes file.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function boot(Router $router)
    {
        parent::boot($router);

        //
    }

    /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    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'); });
    }    
}

我不知道如何将此教程集成到此default login system

提前致谢。

2 个答案:

答案 0 :(得分:3)

前段时间我在给定的教程中和你一样苦苦挣扎。经过研究/搜索,我最终得到了这样的结果。请注意我仍在努力。

第1步:添加您的语言区域
config / app.php 中,在 locales 下添加 skip_locals 'locales' => ['en' => 'English', 'sv' => 'Swedish'],
'skip_locales' => ['admin','auth','etc'],

第2步:修改路线
将map方法更改为以下

public function map(Router $router, Request $request)
{
    $locale = $request->segment(1);
    $this->app->setLocale($locale);
    $skipLocales = $this->app->config->get('app.skip_locales');  

    // If the locale is added to skip_locales array continue without locale
    if (in_array($locale, $skipLocales)) {
        $router->group(['namespace' => $this->namespace], function($router)
        {
            require app_path('Http/routes.php');
        });
    }
    else {
        $router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) {
            require app_path('Http/routes.php');
        });
    }
}

第3步:创建语言中间件
用这个改变内容

<?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 the current local exists
        $locale = $request->segment(1);

        // If the locale is added to to skip_locales array continue without locale
        if (in_array($locale, $this->app->config->get('app.skip_locales'))) {
            return $next($request);
        } else {
            // If the locale does not exist in the locales array continue with the fallback_locale
            if (! array_key_exists($locale, $this->app->config->get('app.locales'))) {
                $segments = $request->segments();
                array_unshift($segments, $this->app->config->get('app.fallback_locale'));
                // $segments[0] = $this->app->config->get('app.fallback_locale');
                return $this->redirector->to(implode('/', $segments));
            }
        }
        $this->app->setLocale($locale);

        return $next($request);
    }

}

希望这会有所帮助!

答案 1 :(得分:0)

对于laravel 5.5&amp;请在https://stackoverflow.com/a/39384200/5180888

上面的解决方案的第2步中更改

编辑app / Providers / RouteServiceProvider.php并更改

public function map(Router $router, Request $request)
{
    $this->mapApiRoutes();
    $this->mapWebRoutes($router, $request);
}

and 
    protected function mapWebRoutes(Router $router, Request $request)
{
    //        Route::middleware('web')
    //            ->namespace($this->namespace)
    //            ->group(base_path('routes/web.php'));

    $locale = $request->segment(1);
    $this->app->setLocale($locale);
    $skipLocales = $this->app->config->get('app.skip_locales');

    // If the locale is added to skip_locales array continue without locale
    if (in_array($locale, $skipLocales)) {
        $router->group([
            'namespace' => $this->namespace, 'middleware' => 'web',
        ], function ($router) {
            require app_path('routes/web.php');
        });
    } else {
        $router->group([
            'namespace' => $this->namespace, 'middleware' => 'web', 'prefix' => $locale
        ], function ($router) {
            require app_path('routes/web.php');
        });
    }
}