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
  • 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')) {
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' => [

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.


// resources/lang/en/messages.php

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

// resources/lang/en_GB/messages.php

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

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


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




Software Developer @andela | Music Lover | Data Science Enthusiast.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Installation-ESP-IDF for Window

Migration of a Workload running in a Corporate Data Center to AWS using the Amazon EC2 and RDS…

Community Update — CIP winners(!), EDCON, zkSync, CHEM@Golem + more

The Real Benefits of User Stories.

Build an Alien Shooter Game in AR

Is 4 or 5 nines of uptime a myth for cloud based services?

Deploy Angular2 Application in Apache on centos 7 server

UWP — Build a download progress

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Olotin Temitope

Olotin Temitope

Software Developer @andela | Music Lover | Data Science Enthusiast.

More from Medium

Laravel | How to extend Notifications and use your custom fields

How to create CRUD operation with CodeIgniter 4 and React JS

Future Scope and Benefits of Laravel in Web Development

JSON Web Tokens in Magento 2.4.4