0

I'm building a multilingual Website using Laravel but I'm facing a problem about Locales.

I have 2 Languages for now (Ar/En) and my routes accept prefix to determine the Language.

I want my routes to be valid if not having a prefix and set a default Locale.

my current code is :

Route::group([
    'prefix' => '/{locale?}',
    'where' => ['locale' => '^(ar|en)$'],
    'middleware' => ['setLocale']
], function(){
    Route::get('/', function () {
        return view('home');
    });

    Route::get('test', function (){
        return 'test';
    });
});

It works for the first route but for any sub-routes its not working if prefix is not provided!

1 Answer 1

1

You could define a fallback route

Route::fallback(function () {
    abort_if(in_array(request()->segment(1), ['ar', 'en']), 404);

    return redirect()->to(url(app()->getLocale().request()->getPathInfo()));
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.