Is there a good package for subdomain laravel translations?

Published 1 week ago by shadrix

I need your help. I'm a bit overwhelmed with localization.

My goals are to have subdomains referencing the current language used. So de.myapp.com for German, fr.myapp.com for French but myapp.com for English.

I thought it would be easy but I just find unmaintained packages. The only one I really like is made by mcamara. But still not perfect but it just adds prefixes like myapp.com/en. (And even if you change it you can't use the middleware because it just adds the prefix...)

Do you have some tips for me? Thank you!

Best Answer (As Selected By shadrix)
lostdreamer_nl

Aah, haha, to shorten the discussion, i've removed the answer then ;)

Anyway, looking at the package, you can just install it, but not use it's build in system to select the language (it's pretty much hard coded to check the first route parameter)

So, after installing it, do not use this part of their code:

Route::group(['prefix' => LaravelLocalization::setLocale()], function() {
    // all your routes here
});

Instead put this in your AppServiceProvider:

    public function boot()
    {
        \LaravelLocalization::setLocale( $this->getSubDomain() );
    }

    private function getSubDomain()
    {
        $domain = preg_replace('#^https?://#', '', request()->root());
        return substr($domain, 0, strpos($domain, '.'));
    }

Also, do not use the two redirect middleware's they give:

'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,

Since boot() is done on every page, and you don't need any parameters because any link will stay within it's own subdomain, this should work just fine.

lostdreamer_nl

[never mind this loooong explanation, it missed the point completely]

shadrix
shadrix
1 week ago (33,210 XP)

@lostdreamer_nl ha sorry I think I was not clear enough, but its really nice thanks!

So what you showed me is how I can translate models. But first I need a good way to route and redirect to the languages etc.

lostdreamer_nl

Aah, haha, to shorten the discussion, i've removed the answer then ;)

Anyway, looking at the package, you can just install it, but not use it's build in system to select the language (it's pretty much hard coded to check the first route parameter)

So, after installing it, do not use this part of their code:

Route::group(['prefix' => LaravelLocalization::setLocale()], function() {
    // all your routes here
});

Instead put this in your AppServiceProvider:

    public function boot()
    {
        \LaravelLocalization::setLocale( $this->getSubDomain() );
    }

    private function getSubDomain()
    {
        $domain = preg_replace('#^https?://#', '', request()->root());
        return substr($domain, 0, strpos($domain, '.'));
    }

Also, do not use the two redirect middleware's they give:

'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,

Since boot() is done on every page, and you don't need any parameters because any link will stay within it's own subdomain, this should work just fine.

shadrix
shadrix
1 week ago (33,210 XP)

@lostdreamer_nl many thanks! It works :) still, need to adjust my needs to it.

And just one little side question, if you have a static page like about us. How would you save the other language, in /lang/de/de.json?

lostdreamer_nl

Put all readable text that you output on that page into your lang files. ie:

  • lang/de/about.json
  • lang/en/about.json
return [
    'title' => 'About us',
    'content' => 'This is our about us page, please read on carefully',
];

And in the view, use the translated strings:

<h1>{!! trans('about.title') !!}</h1>
<p>
{!! trans('about.content') !!}
</p>

Please sign in or create an account to participate in this conversation.