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

Session storage of locale broken in Laravel 5 #173

Closed
dvlpp opened this issue Feb 11, 2015 · 5 comments
Closed

Session storage of locale broken in Laravel 5 #173

dvlpp opened this issue Feb 11, 2015 · 5 comments

Comments

@dvlpp
Copy link

dvlpp commented Feb 11, 2015

The Session value is checked by the LaravelLocalization class BEFORE the Laravel Middleware Illuminate\Session\Middleware\StartSession, which actually initialize le session object, depending on the driver.

This means "locale" is always set back to the default, in every request, by LaravelLocalization (because at this point Session is empty).

@dvlpp
Copy link
Author

dvlpp commented Feb 12, 2015

For me, this won't fix the bug: either I'm missing something, either locale Session is always set back to default. I mean, I just access the project with a localized URL, and then check the session (all OK). Then I access a non localized URL, and locale is set back to default.

Reason is what I mentioned in the bug report: Session value is checked (and set) by LaravelLocalization before the Laravel Middleware which inialize the Session object.

@mcamara
Copy link
Owner

mcamara commented Feb 12, 2015

Can you show me an example of a non-working config file? I tested it with the latest version (1.0.5) and the session is loaded perfectly.

@dvlpp
Copy link
Author

dvlpp commented Feb 12, 2015

Of course, here's my config:

return [
    'supportedLocales' => array(
        'de'          => array('name' => 'German', 'script' => 'Latn', 'native' => 'Deutsch'),
        'en'          => array('name' => 'English', 'script' => 'Latn', 'native' => 'English'),
        'fr'          => array('name' => 'French', 'script' => 'Latn', 'native' => 'français'),
    ),
    'useAcceptLanguageHeader' => true,
    'useSessionLocale' => true,
    'useCookieLocale' => true,
    'hideDefaultLocaleInURL' => false,
];

Here's my L5 Http/Kernel file:

class Kernel extends HttpKernel {

    /**
     * The application's global HTTP middleware stack.
     *
     * @var array
     */
    protected $middleware = [
        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        'Maillon\Http\Middleware\VerifyCsrfToken',
        'Maillon\Http\Middleware\LogSql',
    ];

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => 'Maillon\Http\Middleware\Authenticate',
        'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
        'guest' => 'Maillon\Http\Middleware\RedirectIfAuthenticated',
        'sharp.auth' => 'Maillon\Http\Middleware\SharpAuthenticate',
        'keepCommandeEnCours' => 'Maillon\Http\Middleware\KeepCommandeEnCours',
    ];
}

Thing is: I can reproduce it very easily, and when I'm tracing Session "locale" value it's quite clear: it's well set, but next un-localized request will set it back to default because LaravelLocalization->getSessionLocale() method will return null as it's seems to be called before Session is valued.

@dvlpp
Copy link
Author

dvlpp commented Feb 14, 2015

I'm a bit lost... I think I might have done something wrong, because it seems that nobody else is experiencing this bug, which is, to me, very obvious. I think everything come form routes.php file, with the groupe prefix:

Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
   (...)
}

This LaravelLocalization::setLocale() call always return the default lang (except if locale is present as a segment in the URL) and set it in the session, because Session::get("locale"), at this moment, always returns nothing (session isn't started yet). I can't see how it can work...

If it's from me and I'm missing something obvious, please tell me!

Thanks.

@eickhardt
Copy link

+1

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

3 participants