A Deeper Look at Laravel’s Localization

LaravelPosted on

Laravel’s localization functionality is a huge help when we are developing multilingual applications. But still there are some less known features, so let’s take a more in-depth look to explore them.

First of all, we wrote an article about pushing the translation mechanism to your front-end. Also, we have a package that covers all the functionality on the front-end for you.

Now in this post, we will cover the array based localization, not the JSON one. If you are more interested in the JSON based translations, you can find the docs here.

The Basics

As you know, we have two helper functions, the trans() and the trans_choice(). Both work the same, but while the trans() is suitable for simple language lines, the trans_choice() is for pluralization.

As the first parameter – for both functions – you can give the path to the language line you want to translate with dot notation. For example, you want to translate the failed key in the auth.php; you can pass it like this: trans(‘auth.failed’).

Translation Parameters

In your translations, you can define placeholders that you can easily replace. It can be convenient, primarily when you are working with dynamic strings.

To define a parameter in your translations, all you need to do to prepend a : to your parameter name.

    'message' => 'You are :years years old.',

Then you can easily replace this by passing an array of your parameters to your functions:

trans('messages.message', ['years' => 30]);

// You are 30 years old.
Only those parameters will be replaced, that is given in the array.

There are more. You can transform your parameters. This is a less known feature of Laravel’s localization. You can convert the whole parameter to uppercase or capitalize the first letter. It can be convenient when you are working with names for example.

    'welcome' => 'Hello, :NAME',
    'goodbye' => 'Goodbye, :Name',

Then just before, we need to pass the parameters to replace:

trans('messages.welcome', ['name' => 'pine']);

// Hello, PINE

trans('messages.goodbye', ['name' => 'pine']);

// Goodbye, Pine

Handy, right? All you need to do, to modify your parameters in your languages lines and you are ready to go.


Pluralization is very easy with Laravel. For basic usage, all we need to do to separate the singular and the plural version of the language line with an | character. Then using the trans_choice() function, we need to pass the number that determines if the function uses the singular or plural version.

    'message' => 'One apple|Some apples',

trans_choice('messages.message', 1) // One apple

trans_choice('messages.message', 3) // Some apples

So far this is the way we use it. However, you can define intervals and specify which translation you want to print for an interval. Let’s see an example:

    'message' => '{0} There is nothing|[1,10] There are some items (:number)|[11,*] There are lot of items (:number)',

As you know, we can be specific about a number, or define an interval as well. If you want to specify only the start or the end of the interval, you can use * for the other.

trans_choice('messages.message', 0); // There is nothing

trans_choice('messages.message', 5, ['number' => 5]); // There are some items (5)

trans_choice('messages.message', 300, ['number' => 300]); // There are a lot of items (300)
As the third parameter, we can pass an array to replace the parameters.

Other Features

Some other things you may not know. You can retrieve not only strings but an array of translations. For example, the trans(‘auth’) will return all the translations found in the auth.php file. This is what we use in our package, to map all the translations and bring them to the front-end. Rarely it can be useful.

Another thing is the @lang blade directive. If you are tired of using the trans() function wrapped by a {{  }}, then this directive is for you. It keeps your templates a bit cleaner.

Also, you can use the __() function to retrieve your translations. For example, __(‘messages.message’) is the same like trans(‘messages.message’).


Of course, if you read the docs already, you know all these. But if you are new to this part of Laravel, it’s good to know that the frameworks offer a nice and convenient way to work with localization.

Placeholders, parameter transformation, pluralization, pluralization with intervals and various ways to retrieve what you need. All of these are built in and ready to use.

Need a web developer? Maybe we can help, get in touch!

Similar Posts

More content in Laravel category