DEV Community

Cover image for Part 1 : How to support multiple languages with laravel
Farouk BRAIK
Farouk BRAIK

Posted on

Part 1 : How to support multiple languages with laravel

  1. Part 1 : How to support multiple languages with laravel
  2. Part 2 : How to support multiple languages using laravel's middlewares Coming soon
  3. Part 3 : How to support multiple languages using laravel's models Coming soon
  4. Part 4 : How to support multiple languages using laravel's packages Coming soon

Having laravel as a backend framework has its perks, and one of them is the ability to support multiple languages (locales) through your website.

Today we are going to see how this is going to work, but first of all we are going to create a new laravel application.

By typing laravel new laravel_multi_languages we create a new applicatin:
a laravel new project command through a terminal

we will then open the application with our code editor (in my case VSCode)
opening VSCode inside the application's folder from a terminal

Important Note: I will be using laravel 9, if you have an older version it means that you should find your 'lang' folder in your 'resources' directory.

Laravel localization

Laravel's localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application.

Every laravel application has a language directory somewhere, for laravel 9, a 'lang' directory should be found in the base path of the application, but for older version, it should be found inside the 'resources' directory.
laravel stores its storage in a folder structure, every folder is a locale (like 'en') and then we follow those sub folders with php files that indicates the proper translation.

/lang
    /en
        messages.php
    /es
        messages.php
Enter fullscreen mode Exit fullscreen mode

all we need to use a translation is to use files with the same name for each locale and then use the same key to translate.
Let's see how to get this to work:

inside the 'lang' directory for our new project there is only 'en' locale, but that's okay, it is the default locale of a new laravel application, we will be adding another new locale for our application (Let's say French or 'fr')

the first thing we need to do is to define the supported locales of our application, this way if the user will try to switch locale, we will know if we will let them or not.

. inside our 'config' directory we open the 'app.php' file, after that, we add a new line :

// config/app.php 
...
// here we pass an array of the available locales
'locales' => ['en', 'fr'],

// this is the default locale
'locale' => 'en',
...
Enter fullscreen mode Exit fullscreen mode

. right after that we create a new folder inside the 'lang' directory called 'fr' to use it for french translations, let's also create a new file called 'hello_localization.php' inside both locales' folders

/lang
    /en
        hello_localization.php
    /fr
        hello_localization.php
Enter fullscreen mode Exit fullscreen mode

we want to try and say 'hello' in both french and english, we need a unique key that will refer to 'hello' in both languages, se inside each 'hello_localization.php' folder we return write like this :

// lang/en/hello_localization.php 
<?php

return [
  'hello_message' => 'Hello!'
];
Enter fullscreen mode Exit fullscreen mode

And also for the other language

// lang/fr/hello_localization.php 
<?php

return [
  'hello_message' => 'Bonjour!'
];
Enter fullscreen mode Exit fullscreen mode

As you can see, we gave this 'hello' a name called 'hello_message', this name indicates that the returned value should be a hello message like it says.

So right now, our application knows how to say hello in both languages which is good, but we need to test that out.

. In order to test this out we need to create a new route

// routes/web.php
...
Route::get('/say-hello', function () {
    return __('hello_localization.hello_message');
});
...
Enter fullscreen mode Exit fullscreen mode

as you can see here __() is a helper function that will translate the code giving into the string, in our case we want to display the 'hello_message', but we also need to specify the file that it came from, for that we enter 'hello_localization' (without the '.php') and then writing the translation code that we want to work on.
by calling this function the application will use its own locale and tries to translate it.

the way we access and modify our application's locale is the following

// This is how we modify an application's locale
app()->setLocale($locale);

// This is how we get an application's locale 
app()->getLocale();
Enter fullscreen mode Exit fullscreen mode

Let's serve our application now by using php artisan serve inside a terminal

running a 'php artisan serve' through a terminal

and if we go now to 127.0.0.1:8000/say-hello, we should get a message like this:
Accessing the applications 'say-hello' route in english

Let's say we want to display the application's hello message in french, all we need to do is adding this line in 'web.php' file :

// routes/web.php
...
Route::get('/say-hello', function () {
    app()->setLocale('fr');
    return __('hello_localization.hello_message');
});
...
Enter fullscreen mode Exit fullscreen mode

if we go again to 127.0.0.1:8000/say-hello, we should get a message like this:
Accessing the applications 'say-hello' route in french

Now you have a basic understanding of how localization works, in the next part, I will show you how to remember the users locale choice, and/or prevent the application from updating to a non existing locale.


Important Notice: Kindly leave me a comment or a suggesting that will help me deliver better content to you, your help is greatly appreciated.

Oldest comments (1)

Collapse
 
stormytalent profile image
StormyTalent

Really interesting.
Thanks for sharing!