多國語系 Middleware
發表於 : 2017-09-30 17:17:59
1.Add Lang Floder
2.Add Set Local Session in Router
3.Add Middleware
app/Http/Middleware/Locale.php
4.Add app/Http/Kernel.php
5.Blade
代碼: 選擇全部
resources/lang/jp
resources/lang/tw
代碼: 選擇全部
Route::get('welcome/{locale}', function ($locale) {
App::setLocale($locale);
Session::put('locale', App::getLocale());
return redirect('/');
});
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);
}
}
代碼: 選擇全部
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,
],
代碼: 選擇全部
@lang('messages.Language')