Skip to content

Instantly share code, notes, and snippets.

@naneri
Created March 20, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naneri/591bafa62503ee796ce9 to your computer and use it in GitHub Desktop.
Save naneri/591bafa62503ee796ce9 to your computer and use it in GitHub Desktop.
<?php
// app/filters.php
App::before(function($request)
{
// Set default locale.
$mLocale = Config::get( 'app.locale' );
// Has a session locale already been set?
if ( !Session::has( 'locale' ) )
{
// No, a session locale hasn't been set.
// Was there a cookie set from a previous visit?
$mFromCookie = Cookie::get( 'locale', null );
if ( $mFromCookie != null && in_array( $mFromCookie, Config::get( 'app.locales' ) ) )
{
// Cookie was previously set and it's a supported locale.
$mLocale = $mFromCookie;
}
else
{
// attempt to detect locale from browser.
$mFromBrowser = substr( Request::server( 'http_accept_language' ), 0, 2 );
if ( $mFromBrowser != null && in_array( $mFromBrowser, Config::get( 'app.locales' ) ) )
{
// browser lang is supported, use it.
$mLocale = $mFromBrowser;
} // $mFromBrowser
} // $mFromCookie
Session::put( 'locale', $mLocale );
Cookie::forever( 'locale', $mLocale );
} // Session?
else
{
// session locale is available, use it.
$mLocale = Session::get( 'locale' );
} // Session?
// set application locale for current session.
App::setLocale( $mLocale );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment