Skip to content
Advertisement

Laravel localization in routes without prefix

I have a Laravel app in 9 difference languages:

/resources
-- /lang
-- -- /en_US.json
-- -- /fr_FR.json
-- -- /...

I’m trying to set up localized URL’s like:

Route::get(__("link.projects"), 'GuestController@projects');

All content on the website is displayed in the correct language but routing returns 404 pages except for default fallback language.

My web.php

Route::group(['middleware' => ['web', 'locale']], function () {

  /* GUEST */
  Route::get('/', 'GuestController@index');
  Route::get(__("link.projects"), 'GuestController@projects');
  Route::get(__("link.participants"), 'GuestController@participants');
  Route::get(__("link.participant")."/{public_id}", 'GuestController@participant');
  Route::get(__("link.about"), 'GuestController@about');
  Route::get(__("link.contact"), 'GuestController@contact');
  

});

My locale middleware:

<?php

namespace AppHttpMiddleware;

use Closure;

class SetLocale
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
      if($request->getHost() == "domain.com") {
        $locale = 'en_US';
      } elseif($request->getHost() == "domain.dk") {
        $locale = 'da_DK';
      } elseif($request->getHost() == "domain.de") {
        $locale = 'de_DE';
      } elseif($request->getHost() == "domain.se") {
        $locale = 'sv_SE';
      } elseif($request->getHost() == "domain.fi") {
        $locale = 'fi_FI';
      } elseif($request->getHost() == "domain.es") {
        $locale = 'es_ES';
      } elseif($request->getHost() == "domain.fr") {
        $locale = 'fr_FR';
      } elseif($request->getHost() == "domain.it") {
        $locale = 'it_IT';
      } elseif($request->getHost() == "domain.nl") {
        $locale = 'nl_NL';
      } else {
        $locale = 'en_US';
      }
      if (in_array($locale, [
          "en_US",
          "da_DK",
          "de_DE",
          "sv_SE",
          "fi_FI",
          "es_ES",
          "fr_FR",
          "it_IT",
          "nl_NL"
        ]
      )) {
        session()->put('locale', $locale);
      } else {
        session()->put('locale', 'en_US');
      }

      if(session()->has('locale')) {
          app()->setLocale(session('locale'));
      }

      return $next($request);
    }
}

Using the fallback language URLs in production i’m able to get the content localized but URL’s in the web.php won’t allow translation. I don’t want to have prefix in URL’s since the different locales are set by domain instead.

Advertisement

Answer

Okay so i took a small break and figured out a possible solution which seems to be working perfectly.

$locales = array("en_US","de_DE","es_ES","it_IT","fr_FR","da_DK","sv_SE","fi_FI","nl_NL");
  foreach($locales as $locale) {
    /* GUEST */
    Route::get(__("link.projects", [], $locale), 'GuestController@projects');
    Route::get(__("link.participants", [], $locale), 'GuestController@participants');
    Route::get(__("link.participant", [], $locale)."/{public_id}", 'GuestController@participant');
    Route::get(__("link.about", [], $locale), 'GuestController@about');
    Route::get(__("link.contact", [], $locale), 'GuestController@contact');
    Route::get(__("link.cookies", [], $locale), 'GuestController@cookies');
    Route::get(__("link.terms_of_use", [], $locale), 'GuestController@terms_of_use');
    Route::get(__("link.privacy_policy", [], $locale), 'GuestController@privacy_policy');
    Route::get(__("link.disclaimer", [], $locale), 'GuestController@disclaimer');

  }

Let me know if this has any potential breaks, but for me it’s working.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement