DEV Community

faisal ahmed
faisal ahmed

Posted on • Originally published at Medium

Laravel subdomain specific locale

I needed to change the locale of one of my laravel based projects based on the subdomain. The setup was as follows -

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'); } } } 
Enter fullscreen mode Exit fullscreen mode

Hope this helps someone in future!

Top comments (0)