Route::get('lang/{lang}', [
'as'=>'lang.switch',
'uses'=>'LanguageController@switchLang'
]);
php artisan make:controller LanguageController
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Config, App;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
class LanguageController extends Controller
{
public function switchLang($lang)
{
if (array_key_exists($lang, Config::get('languages'))) {
Session::put('applocale', $lang);
}
return Redirect::back();
}
}
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;
class Language
{
public function handle($request, Closure $next)
{
if (Session::has('applocale') AND array_key_exists(Session::get('applocale'), Config::get('languages'))) {
App::setLocale(Session::get('applocale'));
}
else { // This is optional as Laravel will automatically set the fallback language if there is none specified
App::setLocale(Config::get('app.fallback_locale'));
}
return $next($request);
}
}
protected $middlewareGroups = [
'web' => [
……
\App\Http\Middleware\Language::class,
],
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Session, Config, Request;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// detect Browser Locale and init lang
if (!Session::has('applocale')) {
$lang = substr( Request::server('HTTP_ACCEPT_LANGUAGE') ,0 , 2);
if ($lang == "zh") {
$lang = "cn";
} else {
$lang = "en";
}
Session::put('applocale', $lang);
}
}
<?php
return [
'en' => 'English',
'cn' => '中文',
];
{{-- language switch --}}
@foreach (config('languages') as $lang => $language)
@if ($lang != App::getLocale())
<a href="{{ route('lang.switch', $lang) }}" class="nav-item is-tab is-success">{{$language}}</a>
@endif
@endforeach
/resources/lang/en … /resources/lang/cn Write keyed strings file: auth.php, user.php….
@if(Session::get('applocale') == 'cn') OR @if(App::getLocale() == 'cn')
Blade: @lang('blog.home') Method: __('messages.welcome')