When creating a site it is often in a multi-language perspective. This is called internationalization (i18n) and localization (L10n). Internationalization is about preparing an application to make it potentially suitable for different languages. Localization consists of adding a language-specific component.

This is a rather complex subject that is not limited to the translation of texts but also impacts the representation of dates, the management of plurals, sometimes the layout ...

Language files

There are two ways to manage languages.

Keys and values

We already talked about language files. When Laravel is installed we have this architecture:


It is expected initially only the English language (in) with 4 files. To add a language just create a new folder, for example fr for French.

Obviously we have to have the same content as we will see.

You will not have to do all this translation yourself! Some have already done so with this package. We have already used it in this course.

The sample application ships 2 languages: English and French:




The files present are all exactly the same way. Take the lightest pagination.php, for English we have:


return [
    'previous' => '« Previous',
    'next' => 'Next »',
];

We see that we are content to return a table with keys and values. It can not be easier! If we take the same file in French version:


return [
    'previous' => '« Précédent',
    'next'     => 'Suivant »',
];

Obviously we find the same keys with values adapted to the language.

The texts

The key / value system can quickly become heavy and confusing when you have a lot of texts to translate. Laravel proposes another approach which consists in using the default translation as a key.

If the default language is English for example we will have the English text directly in the code. Other translations will be in JSON files.

If you look at the example application you find a file fr.json:


This file contains elements of this type:

"thinks": "merci",

We have :

Thinks : the default language text in the code

merci : the translation to provide in French

Local configuration

The configuration of the locale is done in the file config / app.php:

'locale' => 'en',

This running locale can be changed if necessary:

App::setLocale('fr');

If a translation is not found in the current locale then we will look for the translation of the default locale also defined in the config / app.php file:

'fallback_locale' => 'en',

We can know the current locale:

$locale = App::getLocale();

In the code

The helper __

Specifically at the code level we use the __ helper. Just specify the key as a parameter (the key for the key-value system and the text in the original language for the second system). For example, this code can be found in the Front / PostController controller of the sample application:

$information = __('Posts for category: ') . '<strong>' . $category->title . '</strong>';

If the locale is in this text will be directly displayed, if the locale is fr it is the corresponding text found in the file fr.json will be displayed:


"Posts for category: ": "Articles pour la catégorie : ",

Blade

You can use the __ helper in a Blade view:

{{ __('thinks') }}

or

@lang('thinks')

conclusion

Laravel has the basic tools for localization.

Laravel offers two complementary systems: key-value or text in the code associated with JSON files.


Login

or Sign Up

Sign Up

or Login
×
tnylea
Cheers Friend!

I'm Tony, the creator of this site. I'm trying to increase my twitter following. Want to follow me?

Follow @tnylea If not, no worries :)