Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Working with Laravel localization

Monday, October 5th, 2015 by Servage

LocalizationWorking with websites in different languages requires a system that can handle content to be displayed depending on a language setting. This setting is usually derived from the user’s profile settings, the client’s browser preference, or maybe the remote IP address country. Either way, as soon as you handle more than one language, you need to deal with a dynamic display. Actually it pays of to implement localization even for just a single language, because the overhead by implementing localization is usually relatively small. However, implementing localization afterwards can be a major issue, because all the code needs to be revisited. It is much faster to implement it from the beginning.

Laravel provides a neat way of dealing with localization. There is a clear and simple way of storing localized content, and there are easy to use helper methods in the framework to display the content to the user.

Storing localized content

The localized content is stored according to the following structure in Laravel:


There can me multiple files in each language folder, which makes it possible to compartmentalize different localized content. I.e. individual software modules can have their own localization files.

The content of such localized content files is simply a PHP array to be returned by the file. This array holds key/value pairs, that can be accessed form the code.

return [
  'startpage_description' => 'Hello, and welcome to our startpage.',

Configuring the language

The locale setting for your application is centrally controlled via the app configuration. You can change the default language in “config/app.php”.

At runtime you can change the locale dynamically, for example as a result of something in the route. This would be a common way to allow different languages to be accessible via the same domain.

Route::get('/{locale}', function ($locale) {

Display localized content

It is pretty straight forward to access the localized content in templates. You can do it both in PHP and blade templates.

// PHP
echo trans('messages.startpage_description');

// Blade
{{ trans('messages.startpage_description') }}

Notice how the file and array key are accessed using dot notation.

Placeholder values in content

Sometimes you need to work with placeholders and substitute them with real values. This can also easily be done.

// Message
'hello' => 'Welcome, :name',

// Display
echo trans('messages.hello', ['name' => 'Your Name']);

Adding values may be one way of working with dynamic data. However, sometimes you also need to output messages that make sense in relation to a given variable value. That process is known as “pluralization”.

// Messages
'users' => 'There is one user|There are many users',

// Display
echo trans_choice('messages.users', 3);

The Laravel localization implementation opens up for most common use-cases with localized content, and this should be a good way for you to implement it. Remember, it really is an excellent idea to include localization right from the beginning (except during prototyping). It saves you a ton of work later!

Working with Laravel localization, 4.5 out of 5 based on 4 ratings
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.