多國語系 Middleware

http://laravel.com/

http://kejyun.github.io/Laravel-4-Docum ... roduction/
回覆文章
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

多國語系 Middleware

文章 yehlu »

1.Add Lang Floder

代碼: 選擇全部

resources/lang/jp
resources/lang/tw
2.Add Set Local Session in Router

代碼: 選擇全部

Route::get('welcome/{locale}', function ($locale) {
    App::setLocale($locale);
    Session::put('locale', App::getLocale());
    return redirect('/');
});
3.Add Middleware
app/Http/Middleware/Locale.php

代碼: 選擇全部

namespace App\Http\Middleware;

use App;
use Closure;
use Config;
use Session;

class Locale
{
    public function handle($request, Closure $next)
    {
        $language = Session::get('locale', Config::get('app.locale'));
        //dd($language);
        App::setLocale($language);
        return $next($request);
    }
}

4.Add app/Http/Kernel.php

代碼: 選擇全部

    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\Locale::class,               
        ],
5.Blade

代碼: 選擇全部

@lang('messages.Language')
回覆文章

回到「laravel」