Loading

Laravel

How to use Localization in Laravel?. The Complete Laravel Developer Course 2023 [Videos].

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.

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.

php artisan make:controller LocalizationController --plain

Step 5 âˆ’ After successful execution, you will receive the following output −

LocalizationController

Step 6 âˆ’ Copy the following code to file

app/Http/Controllers/LocalizationController.php

app/Http/Controllers/LocalizationController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppHttpRequests;
use AppHttpControllersController;

class LocalizationController extends Controller {
   public function index(Request $request,$locale) {
      //sets applications locale
      app()->setLocale($locale);
      
      //Gets the translated message and displays it
      echo trans("lang.msg");
   }
}

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.

Laravel Internationalization

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.

French Example

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.

German Example


See All

Comments (494 Comments)

Submit Your Comment

See All Posts

Related Posts

Laravel / Blog

How to install Laravel ?

For managing dependencies, Laravel uses composer. Make sure you have a Composer installed on your system before you install Laravel. In this chapter, you will see the installation process of Laravel.
14-Feb-2022 /15 /494

Laravel / Blog

What is Laravel - Application Structure?

The application structure in Laravel is basically the structure of folders, sub-folders and files included in a project. Once we create a project in Laravel, we get an overview of the application structure as shown in the image here.
14-Feb-2022 /15 /494

Laravel / Blog

How to setup Configuration in Laravel?

n the previous chapter, we have seen that the basic configuration files of Laravel are included in the config directory. In this chapter, let us discuss the categories included in the configuration.
14-Feb-2022 /15 /494