Oc Chuoj Dau
Oc Chuoj Dau

Reputation: 649

Cannot switch language in Laravel 4

I tried routing to switch language but there's no change. Could you help me, pls?

Route::get('lang/{lang}', function($lang)
{
    App::setLocale($lang);
    return Redirect::to('/');
});

Upvotes: 12

Views: 4323

Answers (2)

Laura Chesches
Laura Chesches

Reputation: 2583

I solved the problem by putting

App::setLocale(Session::get('lang', 'en'));

in app/start/global.php

Upvotes: 1

Phill Sparks
Phill Sparks

Reputation: 20879

App::setLocale() is not persistent - that is to say that it will not remember between requests what you have stored. Instead you could use the session to remember the chosen locale, and read from the session the locale on each request. We can also read the default locale (from config) in case there isn't one set in the session.

// app/routes.php
Route::get('lang/{lang}', function($lang)
{
    Session::put('my.locale', $lang);
    return Redirect::to('/');
});

// app/start/global.php
App::setLocale(Session::get('my.locale', Config::get('app.locale')));

Upvotes: 24

Related Questions