Created
March 15, 2016 04:14
-
-
Save huiralb/30b7e20d4e845e897fa8 to your computer and use it in GitHub Desktop.
Laravel Language ( Localization )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
protected $middlewareGroups = [ | |
'web' => [ | |
\App\Http\Middleware\EncryptCookies::class, | |
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, | |
\Illuminate\Session\Middleware\StartSession::class, | |
\App\Http\Middleware\LanguageMiddleware::class, | |
\Illuminate\View\Middleware\ShareErrorsFromSession::class, | |
\App\Http\Middleware\VerifyCsrfToken::class, | |
], | |
// ... | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Http/Controllers/LanguageController.php | |
namespace App\Http\Controllers; | |
use App\Http\Controllers\Controller; | |
use App\Http\Requests; | |
use Config; | |
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::set('applocale', $lang); | |
} | |
return Redirect::back(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Http/Middleware/LanguageMiddleware.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 LanguageMiddleware | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
if (Session::has('applocale') AND array_key_exists(Session::get('applocale'), Config::get('languages'))) | |
{ | |
$lang = Session::get('applocale'); | |
App::setLocale($lang); | |
} | |
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);; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// config/languages.php | |
return [ | |
'en' => 'English', | |
'fr' => 'Français', | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Http/Controllers/PageController.php | |
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Http\Requests; | |
use App\Http\Controllers\Controller; | |
use Illuminate\Support\Facades\Config; | |
use Illuminate\Support\Facades\Session; | |
class PageController extends Controller | |
{ | |
protected $lang; | |
public function __construct() | |
{ | |
// default locale is en | |
Session::has('applocale') ?: Session::set('applocale', 'en'); | |
$this->lang = Session::get('applocale'); | |
} | |
public function index(Request $request) | |
{ | |
return View('pages.'.$this->lang.'.welcome'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Http/routes.php | |
// ....... | |
/* | |
|-------------------------------------------------------------------------- | |
| Application Routes | |
|-------------------------------------------------------------------------- | |
| | |
| This route group applies the "web" middleware group to every route | |
| it contains. The "web" middleware group is defined in your HTTP | |
| kernel and includes session state, CSRF protection, and more. | |
| | |
*/ | |
Route::group(['middleware' => ['web']], function () { | |
Route::get('/', 'PageController@index'); | |
Route::get('/lang/{lang}', ['as' => 'lang.switch', 'uses'=>'LanguageController@switchLang']); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resources/views/pages/ | |
| fr | |
| home | |
| en | |
| home |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment