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
  • Create a localization middleware

php artisan make:middleware LocalizationMiddleware

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

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;

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.

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.