Created
November 7, 2013 09:53
-
-
Save dkesberg/7352001 to your computer and use it in GitHub Desktop.
Laravel 4 Language Prefixing
based on: https://gist.github.com/dhayab/6172253, http://forums.laravel.io/viewtopic.php?id=7458
This file contains hidden or 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
/* | |
|-------------------------------------------------------------------------- | |
| Languages | |
|-------------------------------------------------------------------------- | |
| | |
| List all valid languages | |
*/ | |
'languages' => array('en', 'de'), |
This file contains hidden or 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
App::before(function($request) | |
{ | |
// locale is set and valid ? | |
if ( in_array(Request::segment(1), Config::get('app.languages')) ) { | |
// set locale in session and continue | |
Session::put('locale', Request::segment(1)); | |
} else { | |
// add locale and redirect | |
return Redirect::to(Config::get('app.locale') . '/' . trim(Request::path(), '/')); | |
} | |
// set app locale to session locale | |
if ( Session::has('locale') ) { | |
App::setLocale(Session::get('locale')); | |
} | |
}); |
This file contains hidden or 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
// get locale from request | |
$locale = Request::segment(1); | |
// prefix routes with current $locale | |
Route::group(['prefix' => $locale], function() | |
{ | |
// language prefixed routes | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment