Recent Topics

Save visitors locale in Session data

Started by on Mar 20, 2013 – Contents updated: Mar 20, 2013

Mar 20, 2013 19:28    

This will allow us to create manual locale selectors.

Example:
- a website visitor opens a link with [b]?locale=en-US[/b] URL param, and this locale is saved into the Session
- now all other pages opened in this session are displayed in en-US locale

Also allow [b]?locale=reset[/b] to switch back to default blog/site locale

Mar 20, 2013 19:55

Here's a simple example, it needs to be extended with locale check to make sure we don't save invalid locales.

// Detect visitor's locale
if( ! $Session->has_User() )
{
	if( $locale = param('locale', 'string') )
	{
		if( $locale == 'reset' )
		{	// Reset saved locale
			$Session->delete('manual_locale');
		}
		else
		{	// Save selected visitor's locale for this session
			$Session->set('manual_locale', $locale);
		}
	}
	// Activate saved locale
	locale_activate( $Session->get('manual_locale') );
}


Form is loading...

Open-Source CMS – This forum is powered by b2evolution CMS, a complete engine for your website.