Hello Everyone,
Today I will share with you how do add multi language to a website that was developed in laravel, my version is 5.6 but it will work on 5.7 as well.
So...here we go!
First of all, I assume that you already have a working version of laravel. Navigate to that particular folder and open your terminal or command prompt.
Run the following command in order to create the required middleware.
$ php artisan make:middleware Localization
Then open it. The file should be in "project_name/app/Http/Middleware/Localization.php"
Update the code that is present with the one below :
public function handle($request, Closure $next)
{
if(\Session::has('locale'))
{
\App::setlocale(\Session::get('locale'));
}
return $next($request);
}
Lets say you want your website to be in English and French.
Go to "project_name/resources/lang/en/"
Create a file name "messages.php"
Create folder "fr" in "project_name/resources/lang/"
Create a file name "messages.php" in "project_name/resources/lang/fr/"
Open "messages.php" in both folder and insert the following codes.
"messages.php" in 'en' folder.
return [
'welcome' => 'Welcome to our application'
];
"messages.php" in 'fr' folder.
return [
'welcome' => 'Bienvenue sur notre application'
];
Most important...
Add the below codes in "project_name/app/Http/Kernel.php" in "protected $middlewareGroups section".
\App\Http\Middleware\Localization::class,
So your codes should be like this :
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\Localization::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
Now all this code in the route, "project_name/routes/web.php" on top of other defined routes.
Route::get('locale/{locale}', function ($locale){
Session::put('locale', $locale);
return redirect()->back();
});
So in order to know the language that is being use, you can simply use :
<html lang="{{ app()->getLocale() }}">
Changing language en/fr
To change the language, just create an anchor tag in html like the following :
<li><a href="{{ url('locale/en') }}" ><i class="fa fa-language"></i> EN</a></li>
<li><a href="{{ url('locale/fr') }}" ><i class="fa fa-language"></i> FR</a></li>
To call the desired text :
<h1>{{ __('messages.welcome') }}</h1>
Latest comments (23)
TL;DR: how to fix a potential "too many redirects" error calling
redirect()->back()Nice article, thanks! Although, if you enter the locale URL directly on the browser URL bar, you could get a "too many redirects" error.
The underlaying problem is, in that particular case, you don't have a "back" URL (as the current URL is the first in the history stack, so Laravel returns the current URL when you call
redirect()->back())You can see it yourself by adding the following:
A quick fix would be to only call the
back()method, if the target URL is different than the current one. You could achieve this by using the following snippet:Cheers!
when i run this code i got an error page not found. the url should be like this 127.0.0.1:8000/localisation/create but when i change the language the url become 127.0.0.1:8000/localisation/lang/en.
Does it also suitable for Laravel 6? Did someone try to do it?
Yeah, I've tried with Laravel 7
Hello, this is good for SEO ??
thank you.
does a with href attr work? as :
how can i add language to url
example example.com/pt-BR/content or example.com/en/content
hey @filippe-felix did you solved this ?
Hi, no =(
laraveldaily.com/multi-language-ro...
youtube.com/watch?v=KqzGKg8IxE4
this will do it :)
Finally its fixed
I tried this and now I got the error as seen in the added screenshot
This is exaclty what I want. Other sites aren't making good explanation about this. Thanks.
If you could share about dynamic multi-language from MySQL, it really appriciate.
Thanks!
thank you very much
Hey, could somebody explain how to fix that? Thanks!!!