Laravel translations in baby steps

translation

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.

--

--

--

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