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):
Spanish language(http://localhost:8000/es):
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:
2. After that, I created file messages.php in both en and es folders:
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.
Recent Comments