Laravel 10 - Localization
Laravel 10 - Localization
In this tutorial, we'll implement localization in Laravel to enable multi-language support for your website, enhancing accessibility and user experience across diverse audiences.
If you're a video person, feel free to skip the post and check out the video instead!
Step # 1 : Create fresh Laravel project or use existing project.
Two commands to create fresh laravel project.
Global Command : laravel new localization
Or use
Non Global Command : composer create-project laravel/laravel --prefer-dist localization
Step # 2 : Access the project.
Open a terminal (e.g., Git Bash) and navigate to your Laravel project's root folder.
Git Bash : cd c:xampp/htdocs/localization
Next, install the required dependencies and run the Laravel Vite development server for front-end assets:
Command : npm install && npm run dev
In a new terminal window or tab (while keeping the Vite server running), navigate to the same project directory to execute further Laravel command.
Step # 3 : Install Localization package.
command : composer require mcamara/laravel-localization
Step # 4 : Publish localization configuration.
Command : php artisan vendor:publish --provider="Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider"
A new configuration file named laravellocalization.php will be created in the config directory of your Laravel project, containing a list of languages supported by the package. To enable specific languages for your project, simply uncomment the ones you want to support. In my case, I will go with the default options, English and Spanish, which are already uncommented.
Step # 5 : Register middleware.
Go to App\Http\Kernel.php and add the following to the protected $middlewareAliases array.
/**** OTHER MIDDLEWARE ****/
'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
'localeCookieRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect::class,
'localeViewPath' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class
Step # 6 : Define Localized route group.
Add the following route group to define all routes that should support localization. Any routes that need to be localized should be placed inside this group.
Route::group(
[
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
], function() {
Route::get('/', function() {
return view('welcome');
});
/** Add other localized routes here **/
});
/** OTHER PAGES THAT SHOULD NOT BE LOCALIZED **/
Step # 7 : Update View.
Replace the content of the welcome.blade.php file with the following HTML for testing purpose.
<!Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="/docs/4.0/assets/img/favicons/favicon.ico">
<title>Localization - Code Shotcut</title>
<link rel="canonical" href="https://getbootstrap.com/docs/4.0/examples/starter-template/">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<!--The {{ __('msg.site') }} will display the localized text for the site key from your language files -->
<a class="navbar-brand" href="#">{{ __('msg.site') }}</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">{{ __('msg.home') }}<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">{{ __('msg.about') }}<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{ __('msg.language') }}</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
<!-- We will change the language based on the user selection -->
<a class="dropdown-item" href="{{ url('/en') }}">En (English)</a>
<a class="dropdown-item" href="{{ url('/es') }}">Es (Spanish)</a>
</div>
</li>
</ul>
</div>
</nav>
<main role="main" class="container mt-5">
<div class="row">
<div class="starter-template">
<h1>Bootstrap starter template</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</div>
</div>
</main>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery-slim.min.js"><\/script>')</script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
Step # 8 : Publish language files to support multiple languages.
php artisan lang:publish
This will create a folder named lang inside the app directory. By default, the lang folder will contain an /en folder.
To support the es language, you need to create an /es folder. It should look like this:
Create a new file named msg.php inside both the lang/en and lang/es folders. The content of the files will differ based on translations.
lang/en
<?php
return [
'site' => 'All About Laravel',
'home' => 'Home',
'about' => 'About',
'language' => 'Language',
//Translation for any other word sentence etc.
];
lang/es
<?php
return [
'site' => 'All About Laravel',
'home' => 'Hogar',
'about' => 'Acerca de',
'language' => 'Idioma',
//Translation for any other word sentence etc.
];
Step # 9 : It's time to test.
Start the Laravel development server.
Command : php artisan serve.
Access below URL
127.0.0.1:8000
The default language is English. You can change it in the config/app.php file by modifying the locale setting if you want.
In order to change the language change it from the dropdown.
Let's change it to Spanish.
Share this with friends!
To engage in commentary, kindly proceed by logging in or registering
Subscribe to Our Newsletter
Stay ahead of the curve! Join our newsletter to see what everyone’s talking about.
0 Comments