Checking if language is set manually

General questions regarding the use of languages in Joomla! 2.5.

Moderators: infograf768, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
ABPAM
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Mon Jan 06, 2014 5:37 pm

Checking if language is set manually

Post by ABPAM » Mon Jan 06, 2014 6:04 pm

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!

User avatar
infograf768
Joomla! Master
Joomla! Master
Posts: 18919
Joined: Fri Aug 12, 2005 3:47 pm
Location: **Translation Matters**

Re: Checking if language is set manually

Post by infograf768 » Wed Jan 08, 2014 2:36 pm

Here is a demo-tutorial of the multilingual settings
http://multilingual.demojoomla.com/mult ... steps.html

You can access back-end to understand better.
Jean-Marie Simonet / infograf · http://www.info-graf.fr
---------------------------------
ex-Joomla Translation Coordination Team • ex-Joomla! Production Working Group

ABPAM
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Mon Jan 06, 2014 5:37 pm

Re: Checking if language is set manually

Post by ABPAM » Thu Jan 09, 2014 10:46 am

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;
	}
}

User avatar
infograf768
Joomla! Master
Joomla! Master
Posts: 18919
Joined: Fri Aug 12, 2005 3:47 pm
Location: **Translation Matters**

Re: Checking if language is set manually

Post by infograf768 » Sat Jan 11, 2014 7:36 am

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.
Jean-Marie Simonet / infograf · http://www.info-graf.fr
---------------------------------
ex-Joomla Translation Coordination Team • ex-Joomla! Production Working Group


Locked

Return to “Language - Joomla! 2.5”