How to create a subdomain for each language define in mcamara/laravel-localization?
Step 1
Update config/laravellocalization.php, add country names which will be used for subdomains, add subdomain key with value "germany":
'de' => ['name' => 'German', 'script' => 'Latn', 'native' => 'Deutsch', 'regional' => 'de_DE','subdomain' => 'germany'],
Step 2
Update RouteServiceProvider.php
public function boot(): void
{
$this->configureLocaleSubdomains();
...
protected function configureLocaleSubdomains()
{
// Define a dynamic pattern for the locale subdomain
Route::pattern('subdomain', 'austria|germany|switzerland');
}
public function map()
{
$this->mapWebRoutes();
$this->mapApiRoutes();
}
protected function mapWebRoutes()
{
// Load routes that match the locale subdomain
Route::domain('{subdmain}.'.config('app.app_domain')) // Define subdomain pattern
->middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php')); // Point to your routes file
}
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
Step 3
Add middleware LanguageToSubdomain.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Route;
class LanguageToSubdomain
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$host = $request->getHost();
/// Split the host by dots
$parts = explode('.', $host);
// Check if the first part is the subdomain
$subdomain = count($parts) > 2 ? $parts[0] : null; // Adjust based on your domain structure
// for local testing
//$subdomain = 'germany';
// Retrieve the localization configuration
$localizationConfig = config('laravellocalization.supportedLocales');
// Get the default locale from Laravel's app configuration
$defaultLocale = config('app.locale'); // This retrieves the default locale
// if this is orig domain
if(empty($subdomain)){
LaravelLocalization::setLocale($defaultLocale);
return $next($request);
}
// find language code for subdomain
foreach ($localizationConfig as $key => $value) {
if($value['subdomain'] == $subdomain){
LaravelLocalization::setLocale($key);
return $next($request);
}
}
// if no subdomain found, use default locale
LaravelLocalization::setLocale($defaultLocale);
return $next($request);
}
}
update Kernel and add this middleware:
'languageToSubdomain' => \App\Http\Middleware\LanguageToSubdomain::class,
Step 4
Add route group in your routes/web.php and put middleware:
Route::group([
//'prefix' => LaravelLocalization::setLocale(),
'middleware' => [
//'localeSessionRedirect',
//'localizationRedirect',
'localeViewPath',
'globalData',
'languageToSubdomain'
]
], function()
{
Route::get('/',[App\Http\Controllers\HomeController::class,'index'])->name('home');
Then you can use your routes without any additional params
Step 5
Create subdomains in your cPanel to point to the root folder, for example
germany.mydomain.com => public_html