I needed to change the locale of one of my laravel based projects based on the subdomain. The setup was as follows -
- www.domain.com (en)
- de.domain.com (de)
- es.domain.com (es)
I implemented the following inside AppServiceProvider to get the job done:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
//subdomain specific locale
$subdomain = current(explode('.', $_SERVER['HTTP_HOST']));
if (in_array($subdomain, ["www","fr","de"])) {
if($subdomain !== 'www'){
app()->setLocale($subdomain);
config('app.url',$subdomain.'.domain.com');
} else {
app()->setLocale('en');
config('app.url','www.domain.com');
}
} else {
app()->setLocale('en');
config('app.url','www.domain.com');
}
}
}
Hope this helps someone in future!
Top comments (0)