Laravel Localization
What is Localization in Laravel?
- Laravel's localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application.
- Language strings are stored in files within the resources/lang directory.
- Localization feature of Laravel supports different language to be used in application.
- You need to store all the strings of different language in a file and these files are stored at resources/views directory.
- You should create a separate directory for each supported language.
- All the language files should return an array of keyed strings as shown below.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts
Example
Step 1
- Create 3 files for languages - English, French and German. Save English file at resources/lang/en/lang.php
Step 2
- Save French file at resources/lang/fr/lang.php
Step 3
- Save German file at resources/lang/de/lang.php
Step 4
- Create a controller called LocalizationController by executing the following command.
Step 5
- After successful execution, you will receive the following output
Step 6
- Copy the following code to file app/Http/Controllers/LocalizationController.php
app/Http/Controllers/LocalizationController.php
Step 7
- Add a route for LocalizationController in app/Http/routes.php file. Notice that we are passing {locale} argument after localization/ which we will use to see output in different language.
app/Http/routes.php
Step 8
- Now, let us visit the different URLs to see all different languages. Execute the below URL to see output in English language.
- http://localhost:8000/localization/en
Step 9
- The output will appear as shown in the following image.
Step 10
- Execute the below URL to see output in French language.
- http://localhost:8000/localization/fr
Step 11
- The output will appear as shown in the following image.
Step 12
- Execute the below URL to see output in German language
- http://localhost:8000/localization/de
Step 13
- The output will appear as shown in the following image.