Codementor Events

Laravel translations in baby steps

Published Mar 22, 2021
Laravel translations in baby steps

When building a new application, it is very important to consider localization. Not because you or your team is looking into expanding into a new market, but the ease of language replacements whenever such need arises.
In this tutorial, I'm going to demonstrate the baby steps to get your app localization working.
The first thing to do is to head over to the Laravel documentation to read more about it. After that, you can follow the steps below;
Create a lang folder under the resources directory

/resources
    /lang
        /en
            messages.php
        /en_GB
            messages.php

Create a localization middleware

php artisan make:middleware LocalizationMiddleware

class LocalizationMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (session()->has('locale')) {
            App::setlocale(session()->get('locale'));
        }
        return $next($request);
    }
}

Add the middleware to the middleware group in the app/Http/Kernel.php file

/**     
 * The application's route middleware groups.     
 *     
 * @var array     
 */    
protected $middlewareGroups = [        
'web' => [            
        \App\Http\Middleware\LocalizationMiddleware::class,        
    ]
];

The above middleware configuration would ensure the localization is present in every web request. So it's pretty easy to flip the switch on the language settings in your application.
Configure a language switcher.

This can be a controller method or a route with a callback. I will try to demonstrate a simple example below;

Route::get('welcome/{locale}', function ($locale) { 
    // Set the selected locale into the session
    session()->put('locale', $locale);
});

What happened in the above example is that each time the language switcher is clicked or the language is appended manually into the URL, the language is added to the session. And the localization middleware will use that as the default for your application.
Access the language from your localization.

The Lang facade gives you access to the language files defined in the lang folder.

<?php

// resources/lang/en/messages.php

return [
    'welcome' => 'Welcome to our organization'
];

<?php

// resources/lang/en_GB/messages.php

return [
    'welcome' => 'Welcome to our organisation'
];

You can get the welcome in the messages language file like below;

Lang::get('messages.welcome')

Thank you for your time reading through this article. I will appreciate your feedback on it.

Discover and read more posts from Olotin Temitope
get started
post comments1Reply
Michael Scott
3 years ago

Thank you for sharing this information. Localization is very important for the application to work in a foreign market. I like your suggestions for solving this issue, but I can hardly do it myself. Therefore, I decided to turn to this source for help https://www.translate.com/ In matters of translation and localization, I always turn to them. With many years of experience in the global market, this definitely deserves my trust.