Laravel: Develop a multiple language / TLD site

Kasper Kloster
2 min readJul 2, 2020

I’m currently working on a website, it’s supposed to have multiple domains and languages, but only one Laravel installation on the server. Of course, I want to test things out locally first.
I couldn’t find a proper solution. I figured something out, that I will share here.

Change localhost domain
The first step is to change which path php artisan will run. So I can test out TLD.

Navigate and edit /etc/hosts
After the line with localhost, add your additional testing domains.

127.0.0.1 localhost site.es site.com

Exit and Save.
Navigate back to url Laravel Project.
You can now specify the host with artisan like this.

php artisan serve --host=site.com --port=8000

And open http://site.com:8000 in your browser. Nice!

Create a custom config file
Under app/ create a new php file. I will call this Example.php and add the code:

<?phpreturn [‘lang’ => ‘en’, 
‘currency’ => ‘USD’,
];

This is our default values.
Whenever you want to get this value, you can type something like this:

return Config::get(‘example.lang’) 

Where “example” is the file name and “lang” is the key. Here “en” will get returned to us.

Create Middleware
Lastly, we will create a middleware that checks TLD.
Go back to your terminal and type:

php artisan make:middleware CheckTLD

Edit our newly created middleware: app/Http/Middleware/CheckTLD.php
I’ve edited the handle method to this:

public function handle($request, Closure $next){ $tld = pathinfo($request->getHost(), PATHINFO_EXTENSION); Config::set(‘app.url’, $request->getHost()); if($tld == ‘es’) {  Config::set(‘example.lang’, ‘es’);
Config::set(‘example.currency’, ‘EUR’);
App::setLocale(‘es’);
} return $next($request);}

First it set the app.url (the APP_URL in .env) to our current host name. eg. site.com
The code will set our config values in, Example.php, if tld is ‘es’.
And it will set the local language (locale => “lang” in config/app.php)

Otherwise default values will be used.

And finally add the new middleware under
app/Http/Kernel.php $middlewareGroups.

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\CheckTLD::class,],‘api’ => [‘throttle:60,1’,\Illuminate\Routing\Middleware\SubstituteBindings::class,],];

Check it out
Add these lines, in one of your blade files (I chose the frontpage) to make a quick test if everything worked.

{{ Config(‘example.lang’) }}
{{ Config(‘example.currency’) }}
{{ Config(‘app.url’) }}

Remember to Run php artisan serve — host=site.com — port=8000 And php artisan serve — host=site.es — port=8000

And that’s it.
Now we have more control over what is going to happen, when you work with different languages, domains, and currencies.

--

--