Page 1 of 1

Checking if language is set manually

Posted: Mon Jan 06, 2014 6:04 pm
by ABPAM
I spent two weeks on how to detect the visitor's country and redirect him to the relevant language (for Russia, Ukraine, Kazakhstan - page in Russian, for Bulgaria - in Bulgarian and in English for all others). Didn't find a Joomla! plugin for that, but found an 'ip to coutry' database and made a simple php script that works. It checks if a cookie named 'language' is set and if not makes a switch($country), sets the cookie and redirects to the appropriate page. But what if the visitor is detected wrongly and wants to change the language manually? I think that this should be something about 'language switcher' module but I don't really know how exactly it works. Any help would be greatly appreciated.
Thanks in advance!

Re: Checking if language is set manually

Posted: Wed Jan 08, 2014 2:36 pm
by infograf768
Here is a demo-tutorial of the multilingual settings
http://multilingual.demojoomla.com/mult ... steps.html

You can access back-end to understand better.

Re: Checking if language is set manually

Posted: Thu Jan 09, 2014 10:46 am
by ABPAM
Thank you, infograf768, but didn't understand anything from the link you gave me. Anyway I solved the problem. How? I check if the language cookie is set. If so, then that means that the user changed the language manually or is already redirected to the language he needs. Here's the script I use:

Code: Select all

$ipAddress = getIP(); //A function that makes a bit deeper IP detection
$db = JFactory::getDbo(); //Create the db object
$query = 'SELECT country FROM ip2nation WHERE ip < INET_ATON("'.$ipAddress.'") ORDER BY ip DESC LIMIT 0,1';
$db->setQuery($query); //Get the country name for this IP address
$results = $db->loadObjectList();

$country = $results[0]->country; //Use a variable for the country name

if(!isset($_COOKIE[JApplication::getHash('language')])) { //Check if Joomla! language cookie is set. If not...
	switch ($country) {
		//All Bulgarian users load the site in Bulgarian
		case 'bg':
			header('Location: http://polytonica.com/bg');
		break;

		//All former Soviet Union countries load the site in Russian.
		/* 1  */case 'ru': //Russia
		/* 2  */case 'am': //Armenia
		/* 3  */case 'az': //Azerbaijan
		/* 4  */case 'by': //Belarus
		/* 5  */case 'ge': //Georgia
		/* 6  */case 'kz': //Kazakhstan
		/* 7  */case 'kg': //Kyrgyzstan
		/* 8  */case 'md': //Moldova
		/* 9  */case 'tj': //Tajikistan
		/* 10 */case 'tm': //Turkmenistan
		/* 11 */case 'ua': //Ukraine
		/* 12 */case 'uz': //Uzbekistan
		/* 13 */case 'ee': //Estonia
		/* 14 */case 'lv': //Latvia
		/* 15 */case 'lt': //Lithuania
			header('Location: http://polytonica.com/ru');
		break;
		
		//All other countries (incl. Anonymous proxy) load the site in English
		default:
			header('Location: http://polytonica.com/en');
		break;
	}
}

Re: Checking if language is set manually

Posted: Sat Jan 11, 2014 7:36 am
by infograf768
For those interested, the table used is available here:
http://www.ip2nation.com/ip2nation

Where did you add your script? It should be a modification in the languagefilter plugin IMHO to take into account the "Language Selection for new visitors" parameter.