Hi,
I don't see a reason why this wouldn't work.
What do you mean by "only the last one is working". What happens when you visit: http://example.dev/ar?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
By using the following sample code when I visit http://app.dev/ar the result of {{ App::getLocale() }} in my relevant view (the view for /ar) is printing en instead of ar.
Note: I am using Laravel 5.4.
Route::group(['prefix' => 'ar', 'namespace' => 'Arabic'], function() {
App::setlocale('ar');
Route::get('/', 'ArabicHomeController@index')->name('arHome');
// ...
});
Route::group(['prefix' => 'en', 'namespace' => 'English'], function() {
App::setlocale('en');
Route::get('/', 'EnglishHomeController@index')->name('enHome');
// ...
});
// Setting default route
Route::get('/', function() {
return redirect()->route('arHome');
});
I have solved my issue as follow:
1 Step. Created a middleware. php artisan make:middleware SetLocale
2 Step. Updated the middleware file.
File: app/Http/Middleware/SetLocale.php
namespace App\Http\Middleware;
use Closure;
use App;
class SetLocale
{
// ...
private $locales = ['ar', 'en'];
// ...
public function handle($request, Closure $next, $locale)
{
if (array_search($locale, $this->locales) === false) {
return redirect('/');
}
App::setLocale($locale);
return $next($request);
}
}
3 Step. Appended my new middleware to app/Http/Kernel.php $routeMiddleware array.
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
// ...
protected $routeMiddleware = [
// ...
'locale' => \App\Http\Middleware\SetLocale::class,
];
}
4 Step. I have used my the middleware in my routes/web.php file.
Route::group(['prefix' => 'ar', 'namespace' => 'Arabic', 'middleware' => 'locale:ar'], function() {
Route::get('/', 'PashtoHomeController@index')->name('arHome');
// ...
});
Route::group(['prefix' => 'en', 'namespace' => 'English', 'middleware' => 'locale:en'], function() {
Route::get('/', 'PashtoHomeController@index')->name('enHome');
// ...
});
Route::get('/', function() {
return redirect()->route('arHome');
});
And that's all.
Thanks, Muhammad Nasir Rahimi
Please or to participate in this conversation.