Volka Dimitrev
Volka Dimitrev

Reputation: 387

How to set different Locales for one Laravel Application pointed to two different domains?

In my laravel application I have two languages, English and French.

there are two domains for my application

test.site for French language
test.live for English language

Now if a user access test.site, application need to be loaded in French

But, if a user access test.live, application need to be loaded in English

My application's locale is by default set to EN.

Now my question is,

How can I set different locales for different domains? OR

How to load English for one domain and French for the other using same laravel application

Upvotes: 0

Views: 1327

Answers (1)

Fitzi
Fitzi

Reputation: 1673

You can set a locale with: App::setLocale($locale);

To get the domain or host, Laravel requests provide a getHost() method, which should return test.site or test.live.

If you want to do this for all or most of your routes, it's probably easiest to create a custom middleware and either register it globally or group the routes you want to apply it to (check the documentation for details about registering).

Your middleware could then look something like this:

public function handle($request, Closure $next)
{
    $locale = $request->getHost() == 'test.site' ? 'fr' : 'en';
    App::setLocale($locale);

    return $next($request);
}

Upvotes: 3

Related Questions