Skip to content

Instantly share code, notes, and snippets.

@dhayab dhayab/filters.php
Last active Jan 25, 2016

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Sep 13, 2013

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

This comment has been minimized.

Copy link

commented Sep 13, 2013

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

This comment has been minimized.

Copy link

commented Sep 25, 2013

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Mar 21, 2014

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

@jordan314

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.