Create Laravel Multi Language Website Step by Step
Share this:
Creating Multi Language website could be a big hassle when you have never created one. In this article I will explain how to make a Laravel multi language application in easy way.
You can also find it on my Github repo Laravel Localization
For this project I will be using English and Arabic Language. After Creating a fresh Laravel project we will have a welcome view file where we are going to start our work. lets copy below code in that file. I am only giving the code inside body tag and I am using bootstrap for this project.
<div class="container my-5">
<div class="row text-center">
<div class="col-12">
{{-- Splitting url so that locale can be set to url and after redirection stays on the same page --}}
@php
$full_url = url()->full();
$locale_url = url(app()->getLocale());
@endphp
<a href="{{ str_replace($locale_url, url("en"), $full_url) }}" class="btn btn-info"> English</a>
<a href="{{ str_replace($locale_url, url("ar"), $full_url) }}" class="btn btn-primary"> Arabic</a>
</div>
<div class="col-12 mt-5">
<h1>{{ __('welcome.title') }}</h1>
<p>{{ __('welcome.subtitle') }}</p>
</div>
</div>
</div>
In this view file we are accessing our locale value using this syntax {{ __(‘welcome.title’) }} which gives us the correct local value according to app locale. All of our locale values will be store in the Laravel ‘lang’ folder inside resources directory. lets create two file welcome.php for our locales one for english and one for arabic.
en/welcome.php
<?php
return [
"title" => "Hello! Welcome.",
"subtitle" => "Localization is Working"
];
?>
ar/welcome.php
<?php
return [
"title" => "مرحبا! مرحبا",
"subtitle" => "التعريب يعمل"
];
?>
You might be wondering what the code inside the @php block is doing. I am splitting the URL so that when users click to change the language we can put the locale in the URL and users stay on the same page after redirection. For example if users are in this URL https://localhost/en/posts/slug then when click Arabic the URL should be https://localhost/ar/posts/slug .
Our Route.php file will look like this
Route::get('/', function () {
return redirect(app()->getLocale());
});
Route::group([
'prefix' => '{locale}',
'where' => ['locale' => '[a-zA-Z]{2}'],
'middleware' => 'setlocale',
], function() {
Route::get('/', function(){
return view('welcome');
});
});
The slash route in the above code redirect the user to the current locale. After redirection the slash route will be http://localhost/en . Now here the most important part comes in the middleware part. We have created a setlocale middleware which does the job for adding locale to our all routes inside that group. The middleware codes are
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
class setLocale
{
public function handle(Request $request, Closure $next)
{
app()->setLocale($request->segment(1));
URL::defaults(['locale' => $request->segment(1)]);
return $next($request);
}
}
In the above code we are setting the locale to your Laravel application so that we can get the local everywhere in the project using app()->getLocale() and also we are setting the local in our URL defaults
and dont forget to put it in our karnel.php routemiddleware
protected $routeMiddleware = [
...
'setlocale' => \App\Http\Middleware\SetLocale::class,
];
Note:
That’s it now we have a Laravel multi language app. but there might occur a small problem that is whenever you use your controller function with some route parameters The locale stays on the first parameter. For example if I have a function show like this
public function show($id){
}
Then it will be something like this
public function show($locale, $id){
}
// With Request
public function show(Request $request, $locale, $id){
}
Thanks! for Staying with me till the bottom of this page