Skip to content

Instantly share code, notes, and snippets.

@dhayab
Last active January 25, 2016 16:52
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dhayab/6172253 to your computer and use it in GitHub Desktop.
Save dhayab/6172253 to your computer and use it in GitHub Desktop.
Laravel persistent language prefixing
<?php
App::before(function($request))
{
if ( in_array(Request::segment(1), Config::get('app.languages')) ) {
Session::put('locale', Request::segment(1));
} else {
return Redirect::to(Config::get('app.locale'));
}
if ( Session::has('locale') ) {
App::setLocale(Session::get('locale'));
}
});
<?php
Route::group(array('prefix' => '{locale}')
Route::get('home', function ( )
{
return View::make('home');
});
)->where(array('locale' => '[a-z]{2}'));
@RomainSauvaire
Copy link

Hello dhayab,

Nice work here !

Have you any idea on how to "update" the URL helper to make it write the language prefix each time he is used ?

Regards !

@RomainSauvaire
Copy link

Anyway, the

->where(array('locale' => '[a-z]{2}'));

In routes.php does not seem to work on a Route::group. Too bad you must apply it on each route :s

@ranaanees
Copy link

This works perfect for me....and includes lang code in url....

$languages = array('en', 'ar');
$locale = Request::segment(1);
if (in_array($locale, $languages)) {
    \App::setLocale($locale);
} else {
    $locale = null;
}

Route::group(array('prefix' => $locale), function()
{
    Route::get('/', array('as' => 'home', 'uses' => 'HomeController@getIndex'));            
});

my default language is English so I dont include en in url....but when Arabic ar in the url...

@kuroski
Copy link

kuroski commented Jan 8, 2014

That's awesome.

Sorry for the poor formulation of my text.
My English is not very good.

I just did a small modification.
If there is a value on session "locale" variable, and the locale was erased from url, It sets the url for the locale session

filters.php

App::before(function($request)
{
    if ( in_array(Request::segment(1), Config::get('app.languages')) ) {
        Session::put('locale', Request::segment(1));
    } elseif( Session::has('locale') ) { // -------------- HERE!
        return Redirect::to(Session::get('locale'));        
    }else {
        return Redirect::to(Config::get('app.locale'));
    }
    if ( Session::has('locale') ) {
        App::setLocale(Session::get('locale'));
    }
});

routes.php

Route::group(array('prefix' => '{locale}'), function()
{
    Route::get('/', array('as' => 'home', function()
    {
        return View::make('home');
    }));
});

@notforever
Copy link

Hi,

any idea of why I am getting the error in filter.php:

in_array() expects parameter 2 to be array, null given

  if ( in_array(Request::segment(1), Config::get('app.languages')) ) {

I am trying to access the site like this localhost/es/finder but even /localhost/finder I get the error.

Thanks

@alana314
Copy link

alana314 commented Nov 5, 2014

@kuroski thank you.
@notforever to answer your old question, add something like this to app/config.php:
'languages' => Array('en', 'es', 'it', 'de', 'jp')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment