Laravel Localization

We'll learn how to properly handle multilanguage in Laravel.
If your audience from different languages you must have a multilanguage feater on your website, so the laravel supports multilanguage wich called localization.

Laravel supports this technology well by organizing files and retrieving them as we will know them.

The first thing you should do is create a specific  folder for each language
Inside each folder create a file containing the texts

the default language directory in the resources/lang directory

/resources
    /lang
        /ar
            text.php
        /en
            text.php

as you can see, we create a two folder ar for the Arabic language and en for the English language, within these folders we create a language strings file.

/ar/text.php

<?php

return [
    'home' => 'الرئيسية',
    'shope' => 'تسوق',

];

/en/text.php

<?php

return [
    'home' => 'home',
    'shope' => 'shope',

];

How to determine which Language to use

How will we make Laravel to determine which Language to use Arabic or English?

The default language is en for your application and you can check or change it from the configuration file config/app.php .

laravel localization

also, you can change the language at run time using the setLocale method

...
 App::setLocale('ar');
...

Retrieving Translation Strings

There are two way to retrieving translation strings in laravel 

  • using __ method
  • using @lang directive.

example of using __ method

echo __('text.home');

example of using @lang directive.

@lang('text.home')