Categories

Friday, April 19, 2024
#919814419350 therichposts@gmail.com
LaravelLaravl 5.7

Best Practices for Laravel Localization

Best Practices for Laravel Localization - The Rich Post

Hello to all, welcome to therichpost.com. In this post, we will do Best Practices for Laravel Localization.

I know this post, you can easily find anywhere but the main purpose to share this post is better and easily understand without to much documentation.

I am doing this in Laravel 5.7.

With Laravel Localization, we can make my website or application multi-language.

Most of my friends say, you write very less and you just share the code but I like this thing.

Here is the working pictures for both the languages English and Spanish:
English Language(http://localhost:8000/en):

english_language

 

Spanish language(http://localhost:8000/es):

spanish_language

 

Now here i am sharing the working and easy steps:

 

1. Very first, I created two folders(en, es) in resources\lang:

Here you can see clearly:

language_folders

 

 

2. After that, I created file messages.php in both en and es folders:

mesage_file_en

mesage_file_es

 

3. Now, I added below code for messages.php in resources\lang\es folder:
<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Pagination Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines are used by the paginator library to build
    | the simple pagination links. You are free to change them to anything
    | you want to customize your views to better match your application.
    |
    */

    "I love programming." => "Me encanta programar."

];

 

 

4. Now, I added below code for messages.php in resources\lang\en folder:
<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Pagination Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines are used by the paginator library to build
    | the simple pagination links. You are free to change them to anything
    | you want to customize your views to better match your application.
    |
    */

    "I love programming." => "I love programming."

];

 

5. To call messages.php text, I added below code in resources\views\welcome.blade.php file:
<center><h1>{{ __('messages.I love programming.') }}<h1></center>

 

6.  Finally, I added the below code in routes\web.php file:

In this, I made new welcome page route with setLocale function:

Route::get('/{locale}', function ($locale) {
    App::setLocale($locale);

    return view('welcome');
});

 

7. Finally I ran below command into terminal:
php artisan serve
execute urls:

http://localhost:8000/es // for spanish

http://localhost:8000/en // for english

If you have any query related to this post, then do comment or ask questions.

 

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.