Marwelln
6108
47
Tips

Example on how to use multiple locales in your Laravel 5 website

Posted 4 years ago by Marwelln

The following will make domain.com/{locale} available to your website.

What you need to use multiple locales is three things.

  1. Add list of available locales to an array.
  2. Prefix all your routes with current locale.
  3. A language middleware that checks if user is using a valid locale.

Step 1: Add your locales

Open up config/app.php and add the following below 'locale' => 'xx'.

'locales' => ['en' => 'English', 'sv' => 'Swedish'],

Modify this array with your desired locales.

Example at Github

Step 2: Prefix your routes

To prefix our routes, we change the map method in app/Providers/RouteServiceProvider.php. Change the map method to the following:

public function map(Router $router, Request $request)
{
    $locale = $request->segment(1);
    $this->app->setLocale($locale);

    $router->group(['namespace' => $this->namespace, 'prefix' => $locale], function($router) {
        require app_path('Http/routes.php');
    });
}

Then add this to the top of the file:

use Illuminate\Routing\Router;
use Illuminate\Http\Request;

Example at Github

Step 3: Create a language middleware

Now create a file named Language.php in app/Http/Middleware with this content:

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Routing\Redirector;
use Illuminate\Http\Request;
use Illuminate\Foundation\Application;
use Illuminate\Contracts\Routing\Middleware;

class Language implements Middleware {

    public function __construct(Application $app, Redirector $redirector, Request $request) {
        $this->app = $app;
        $this->redirector = $redirector;
        $this->request = $request;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Make sure current locale exists.
        $locale = $request->segment(1);

        if ( ! array_key_exists($locale, $this->app->config->get('app.locales'))) {
            $segments = $request->segments();
            $segments[0] = $this->app->config->get('app.fallback_locale');

            return $this->redirector->to(implode('/', $segments));
        }

        $this->app->setLocale($locale);

        return $next($request);
    }

}

Example at Github

Now make that middleware run on all requests by adding it to the $middleware property in app/Http/Kernel.php. It is recommended to add it to the top of the array.

protected $middleware = [
    'App\Http\Middleware\Language',
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',
    'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken',
];

Example at Github

You are now set to go.

Example repo

I have an example repository available at https://github.com/Marwelln/laravel-multiple-locales – clone it to test it out or if you are unsure how it works.

Please sign in or create an account to participate in this conversation.