Support the ongoing development of Laravel.io →
posted 10 years ago
Requests
Last updated 2 years ago.
0

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?

0

no ! you declare $local in global.php and use it in Routes.php ?

Can you copy both files in bin for me ?

Thanks

0

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....

Last updated 10 years ago.
0

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 :)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

RamyarIran ramyariran Joined 18 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.