1

What is the best way to setup a multi language website with Laravel? The URL must contain the language (nl, fr, en). For example: mywebsite.com/en/faq. Most examples and tutorials I find use the session to store the current language which is completely useless of course. I should be able to directly link to a page in a specific language.

I could create a route per language but that does not really seem like a good idea. Ideally this could be made dynamic in order to easily create more locales.

Matthias
  • 141
  • 3
  • 11
  • Can you check this post: [How to create Laravel localization](https://devnote.in/how-to-create-laravel-localization/) – Fefar Ravi May 18 '22 at 10:29
  • @FefarRavi I'm sure this will work in a way but the locale is stored in a parameter instead of in the url path itself. This uses a new router just to change the locale and this is just too restrictive for my needs. Thanks for replying :) – Matthias May 18 '22 at 15:13

4 Answers4

1

I ended up using the laravel-localization package by mcamara. Seems to do everything I need. I'm not really sure why anyone would try to build their own version if this exists.

Matthias
  • 141
  • 3
  • 11
0

You can do something like this, create route with locale group and locale middleware

Route should look like this enter image description here

And then in your middleware set the language based on the locale parameter.

We can pick dynamic url segment using request()->route('')

enter image description here

Note: You need to pass locale value when you call route() helper.

Maybe there is another better approach to it.

Akhzar Javed
  • 616
  • 6
  • 12
0

I'm curious, why you cannot use session (it's a true question, I really would like to understand)?

You can use the same approach than with session: create a middleware to set App::setLocale("YourLanguage") based on query string.

public function handle(Request $request, Closure $next)
{
    // This example try to get "language" variable, \
    // if it not exist will define pt as default \
    // you also can use some config value: replace 'pt' by Config::get('app.locale')

    App::setLocale(request('language', 'pt'));

    return $next($request);
}

Or you can do it by Route:

// This example check the if the language exist in an array before apply
Route::get('/{language?}', function ($language = null) {
    if (isset($language) && in_array($language, config('app.available_locales'))) {
        app()->setLocale($language);
    } else {
      app()->setLocale(Config::get('app.locale'));
    }
    
    return view('welcome');
});

Source:

Laravel change language depending on a $_GET parameter

https://lokalise.com/blog/laravel-localization-step-by-step/

Juranir Santos
  • 370
  • 2
  • 6
  • Doing it with a session just seems really weird because this should not be linked to a session. The language is defined by the URL. If I post an URL on social media in a specific language I want people to go the that exact language. I don't want them to go to the default language first where they need to find the language switch. Honestly I'm a little confused why a well known PHP framework has no built in way to handle this. Everyone seems to throw something together in a different way in order to have a multi language website. – Matthias May 16 '22 at 13:26
0

You can use the Mcamara Laravel localization package. it'll do everything you need. And here's the package link:

https://github.com/mcamara/laravel-localization