During a recent project, we faced the issue of adding a multi-language functionality to our application.

We store our Laravel applications on DigitalOcean, but this tutorial will work basically on any other hosting company.

Using Laravel middlewares we found a quite easy and straight forward solution.

In this tutorial we will show you how to easily create your own.

Add available locales in the configurations

In order to allow only certain locales to work , simple add right below the locale key  in config/app.php the following array:

'locales' => [
    'en' => 'English',
    'it' => 'Italiano'],

Create a custom middleware

First let’s create a custom middleware running the following command.

php artisan make:middleware Language

This will create Language.php in App\Http\Middleware. Modify the handle method adding your own logic, in our example we will use a query parameter _locale to modify the application locale and set a _locale session variable to store this choice and maintain the locale across various pages.

public function handle($request, Closure $next)
{
    $locales = config('app.locales');
    $locale = $request->get('_locale');
    if(!is_null($locale) && is_array($locales) && array_key_exists($locale, $locales)){
        app()->setLocale($locale);
        session(['_locale'=>$locale]);
    }elseif(!is_null(session('_locale'))){
        app()->setLocale(session('_locale'));
    }
    return $next($request);
}

Modify the middleware web group

Now we just need to tell Laravel how to use the our class. We found that adding this middleware to the “web” middleware group was pretty comfortable so , open the App\Http\Kernel and insert your Language class declaration in the web array like the code below:

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \App\Http\Middleware\Language::class
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

That’s it, now you can change your app locale simply using ?_locale=it in the url.

 

Laravel 5.3 add multi-language functionality 2016-11-24T12:08:09+00:00 Soipo

Comments are closed.

Privacy Preference Center

gdpr

We track users consent by creating a cookie and storing their preferences there.

soiposervices.com

_ga

Used to distinguish users.

soiposervices.com

_gat

Used to throttle request rate. If Google Analytics is deployed via Google Tag Manager, this cookie will be named _dc_gtm_.

soiposervices.com

_gid

Used to distinguish users.

soiposervices.com

lang

Used to understand the locale used by the user and present the right content.

cdn.syndication.twimg.com
Used to understand the locale used by the user and present the right content.

tk_lr

Collection of internal metrics for user activity, used to improve user experience.

soiposervices.com

tk_or

Collection of internal metrics for user activity, used to improve user experience.

soiposervices.com

tk_r3d

Collection of internal metrics for user activity, used to improve user experience.

soiposervices.com

NID

Used to distinguish users.

google.com
Used to distinguish users.

Close your account?

Your account will be closed and all data will be permanently deleted and cannot be recovered. Are you sure?

%d bloggers like this: