Skip to main content

Laravel – Localization

Laravel – Localization

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.
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.
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.
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
Step 13 − The output will appear as shown in the following image.
German Example

Popular posts from this blog

What is Lumen?

What is Lumen? Lumen  is PHP micro-framework that built on Laravel’s top components.It is created by Taylor Otwell. It is perfect option for building Laravel based micro-services and fast REST API’s. It’s one of the fastest micro-frameworks available. You can install Lumen using composer by running below command 1 composer create - project -- prefer - dist laravel / lumen blog

Laravel – Response

Laravel – Response A web application responds to a user’s request in many ways depending on many parameters. This chapter explains you in detail about responses in Laravel web applications. Basic Response Laravel provides several different ways to return response. Response can be sent either from route or from controller. The basic response that can be sent is simple string as shown in the below sample code. This string will be automatically converted to appropriate HTTP response. Example Step 1  − Add the following code to  app/Http/routes.php  file. app/Http/routes.php 1 2 3 Route :: get ( '/basic_response' , function ( ) {    return 'Hello World' ; } ) ; Step 2  −  Visit  the following URL to test the basic response. 1 http : //localhost:8000/basic_response Step 3  − The output will appear as shown in the following image. Attaching Headers The response can be attached t...

Which template engine Laravel use ?

Which template engine Laravel use ? Laravel uses Blade Templating Engine. Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file extension and are typically stored in the resources/views directory.