That's not really a good place for that code... Better try to put this code in one of you start files, for example, at the end of app/start/global.php (Laravel 4.2) so it would looks like:
// app/start/global.php
$languages = array('en', 'ar');
$locale = Request::segment(1);
if (in_array($locale, $languages)) {
\App::setLocale($locale);
} else {
$locale = null;
}
Just tested and it work for me... Is it also working for you?
no ! you declare $local in global.php and use it in Routes.php ?
Can you copy both files in bin for me ?
Thanks
Here are the two pieces of code:
app/start/global.php
$languages = ['en','ar'];
$locale = Request::segment(1);
if( in_array($locale, $languages) ){
App::setLocale($locale);
}
else {
$locale = null;
}
app/routes.php
Route::group( ['prefix' => App::getLocale() ], function()
{
Route::get('/', function(){
return 'Hi, your locale is '. App::getLocale();
});
});
Simple make use in routes.php of App::getLocale() to get the current locale...
Watch out! When going to yourdomain.com/ertyu for example, the locale will be null and you will see a NotFoundHttpException. Perhaps set the locale to 'en' in your else block if you don't want an error....
Thanks
My problem was testing codes in Routes.php in my package for front end ... now i tried in main Routes.php in App folder and it worked ... thanks :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community