Location: PHPKode > scripts > Colossal Mind Language Detector > example-2.php
<?php


ini_set('display_errors',1);


// Example 2
// DESCRIPTION:
// Upon 1st request, the script detects the language and redirects to
// a URI with the language parameter in the query. The CMLANG cookie is also
// set.
// If you click on a link to another language, the new query string
// will have the code of the new language. While the the page is loading,
// the script will set a new CMLANG cookie.


//----------------------------------------------------------------------

// MAP browser header Accept-Language to language
// is used for detecting language setting from browser's
// Accept-Language header
$lang_params['LANG_LANG_MAP'] = array(
							'en*' => 'en_GB',
							'de*' => 'de_DE',
							'cs*' => 'cs_CZ',
							'sk*' => 'cs_CZ',
							'it*' => 'it_IT',
							'nl*' => 'nl_NL',
							'fr*' => 'fr_FR',
							'es*' => 'es_ES',
							'pt*' => 'pt_PT'
							);

// OPTIONAL but recommended if your system can detect country from IP address.
// MAP country code to language
// If you don't want to use this, don't define it.
$lang_params['LANG_GEO_MAP'] = array(
							'at' => 'de_DE', // Austria
							'ch' => 'de_DE', // Switzerland
							'de' => 'de_DE', // Germany
							'li' => 'de_DE', // Liechtenstein
							'cz' => 'cs_CZ', // Czech Republic
							'sk' => 'cs_CZ', // Slovakia
							'it' => 'it_IT', // Italy
							'nl' => 'nl_NL', // Netherlands
							'be' => 'fr_FR', // Belgium
							'fr' => 'fr_FR', // France
							'mc' => 'fr_FR', // Monaco
							'lu' => 'fr_FR', // Luxembourg
							're' => 'fr_FR', // Reunion
							'es' => 'es_ES', // Spain
							'co' => 'es_ES', // Colombia
							'pe' => 'es_ES', // Peru
							've' => 'es_ES', // Venezuela
							'ec' => 'es_ES', // Ecuador
							'gt' => 'es_ES', // Guatemala
							'cu' => 'es_ES', // Cuba
							'bo' => 'es_ES', // Bolivia
							'hn' => 'es_ES', // Honduras
							'py' => 'es_ES', // Paraguay
							'sv' => 'es_ES', // El Salvador
							'cr' => 'es_ES', // Costa Rica
							'pa' => 'es_ES', // Panama
							'gq' => 'es_ES', // Equatorial Guinea
							'mx' => 'es_ES', // Mexico
							'ar' => 'es_ES', // Argentina
							'cl' => 'es_ES', // Chile
							'do' => 'es_ES', // Dominican Republic
							'ni' => 'es_ES', // Nicaragua
							'uy' => 'es_ES', // Uruguay
							'pt' => 'pt_PT', // Portugal
							'br' => 'pt_PT', // Brazil
							'mz' => 'pt_PT', // Mozambique
							'ao' => 'pt_PT', // Angola
							'gw' => 'pt_PT', // Guinea-Bissau
							'tl' => 'pt_PT', // East Timor
							'cv' => 'pt_PT', // Cape Verde
							'st' => 'pt_PT' // São Tomé and Príncipe
							);

// Default language
// If you leave it empty or not define it, default will be en_GB.
$lang_params['LANGUAGE_DEFAULT'] = 'en_GB'; // Example 'fr_CA' (French language, Canada)

// If you can tell me which country the user is in, then plug the 2-letter code here.
// There are number of systems (or classes) available that convert IP address to country code.
// You can leave it empty (or not define it) and this class will work fine.
$lang_params['COUNTRY_CODE'] = 'us'; // case-insensitive

// If you enable to use cookie for storing the language value, any
// subsequent language detection will be faster and more likely correct.
// If you leave it empty or not define it, default will be false (not use cookie).
$lang_params['USE_COOKIE'] = true;

// OPTIONAL: Name of the language cookie.
// If you leave it empty or not define it, default will be 'CMLANG'.
$lang_params['COOKIE_NAME'] = 'CMLANG';


//----------------------------------------------------------------------

// it is good to know the name of each language

$lang_all_languages = array(
							'en_GB' => 'English',
							'de_DE' => 'Deutsch',
							'cs_CZ' => 'čeština',
							'it_IT' => 'Italiano',
							'nl_NL' => 'Nederlands',
							'fr_FR' => 'français',
							'es_ES' => 'español',
							'pt_PT' => 'portugués'
							);

//----------------------------------------------------------------------



if (!isset($_GET["lang"]) || !array_key_exists($_GET["lang"], $lang_all_languages)) {

	require('class.colossal-mind-language-detector.php');
	$cmldet = new colossal_mind_language_detector($lang_params);
	$language_code = reset($cmldet->detect_language());

	$url = reset(explode('?', $_SERVER["REQUEST_URI"])); // remove query string, if any
	header('Location: '. $url .'?lang='. $language_code); // the newly detected language in the query string
	exit();

}
else {

	// check if language has changed
	if ((isset($_COOKIE[$lang_params['COOKIE_NAME']]) && $_COOKIE[$lang_params['COOKIE_NAME']] != $_GET["lang"]) || (!isset($_COOKIE[$lang_params['COOKIE_NAME']]) &&$lang_params['USE_COOKIE'])) {
		// write new cookie
		setcookie($lang_params['COOKIE_NAME'], $_GET["lang"], (time()+10*365.25*86400), "/", $_SERVER['HTTP_HOST']);
	}

	header("Content-type: text/html; charset=UTF-8"); // necessary to display UTF-8 characters correctly

	echo '<html>
	<head></head>
	<body>';

	echo 'Language of this page: '.$lang_all_languages[$_GET["lang"]].'<br /><br />';

	$url = reset(explode('?', $_SERVER["REQUEST_URI"])); // remove query string, if any

	echo '<ul>';
	foreach ($lang_all_languages as $key => $val) {
		if ($_GET["lang"] != $key) echo '<li><a href="'.$url.'?lang='.$key.'">'.$val.'</a></li>';
	}
	echo '</ul>';

	echo '</body></html>';

}


?>
Return current item: Colossal Mind Language Detector