Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working with cached routes #201

Closed
Casinelli opened this issue Apr 13, 2015 · 5 comments
Closed

Not working with cached routes #201

Casinelli opened this issue Apr 13, 2015 · 5 comments

Comments

@Casinelli
Copy link

Any way to make it work with cached routes? (using php artisan route:cache)

@christiaan-lombard
Copy link

Not sure if it will work with sessions and route translations, but try this:

In your routes.php, set the localized group prefix to '{locale?}', instead of Localization::setLocale(), and add set_locale middleware before other localization middlewares:

Route::group(

  [
    'middleware' => ['set_locale', 'localizationRedirect', 'localeSessionRedirect'],
    'prefix' => '{locale?}'
  ],

  function()
  {
      Route::get('/',         'HomeController@getIndex');
  }
);

The set_locale middleware, added to your kernel, will grab the locale? parameter and set it as the current locale:

class SetLocale implements Middleware {


  public function handle($request, Closure $next)
  {

    $locale = $request->route('locale');
    Localization::setLocale($locale);
    return $next($request);
  }
}

@Casinelli
Copy link
Author

Thanks! It works for setting up the routes correctly, but now using URL::route() gives the link without including the locale. For example using the 'it' language, and doing URL::route('profile'), I get /profile, instead of /it/profile. Trying to override the UrlGenerator now, though not sure it's the best way to go about it.

@christiaan-lombard
Copy link

Yeah, I can see how this solution could break a few things. A workaround is to send the current locale as a parameter to the url generator.

URL::route( 'profile', [ 'locale' => LaravelLocalization::getCurrentLocale() ] )

I made this helper function to get a localized URL:

function route_local($name, $parameters = array(), $absolute = true, $route = null)
  {

    $parameters = array_merge($parameters, ['locale' => (string) LaravelLocalization::getCurrentLocale()]);

    return app('url')->route($name, $parameters, $absolute, $route);
  }

@arjankapteijn
Copy link

I'm having the same problem but change the route generates an error. "Class set_locale does not exist".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants