Change language automatically according to the visitor country

Started by Elmacik, February 21, 2006, 09:28:30 PM

Previous topic - Next topic

Elmacik

Edit: I optimized it, re-wrote the code.

Thanks to BBClone for making a good software.
Notes: This script will change the language of the site automatically according to the country of the visitor. Of course doing it by the language of the browser is much faster, I will do it too, some time.

First, download the IP database from here:
http://bbclone.de/download.php?sub=ip2ext&get=ip2ext-20070131.zip
Unzip and upload to your site in a folder. For example name it IPs.

Open you Load.php which is located in Sources folder.
Put the code below, to the end of the file, before ?> signs.


function IP_ULKE()
{
global $language, $user_info;

// Directory of the IP database.
$IP_DB = '/home/content/e/l/m/elmacik2/html/s/stats/ip2ext/';

// If the user already selected a language, dont do anything..
if ($user_info['language'] != $language)
return;

// The languages we want to serve. Edit according to your willing.
$diller = array(
'tr' => 'turkish',
'en' => 'english',
'es' => 'spanish',
'de' => 'german',
'fr' => 'french',
'ru' => 'russian',
'empty' => '', // To load the default language if something goes wrong.
);

$dosya = $IP_DB.(substr($user_info['ip'], 0, strpos($user_info['ip'], ".")).".inc");
$ip_uzun = ip2long($user_info['ip']);
$ip_uzun = sprintf("%u", $ip_uzun);
$fp = fopen($dosya, "rb");

while (($aralik = fgetcsv($fp, 32, "|")) !== false)
{
if (($ip_uzun >= $aralik[1]) && ($ip_uzun <= ($aralik[1] + $aralik[2] - 1)))
{
$dil = in_array($aralik[0], array_keys($diller)) ? $aralik[0] : 'empty';
break;
}
}
fclose($fp);
return $diller[$dil];
}


In the code, dont forget to change this line according to yourself:
$IP_DB = '/home/users/html/IPs/';

And of course, edit the languages which you will serve:

// The languages we want to serve. Edit according to your willing.
$diller = array(
'tr' => 'turkish',
'en' => 'english',
'es' => 'spanish',
'de' => 'german',
'fr' => 'french',
'ru' => 'russian',
'empty' => '', // To load the default language if something goes wrong.
);


The two letters code of the languages must be valid. Here is the list of the valid abbreviations of the languages:
http://www.w3.org/WAI/ER/IG/ert/iso639.htm

Open index.php which is located in the main SMF directory.
Find:
loadUserSettings();
Add after:


// If lang is selected manually (i.e index.php?language=sth), dont do anything.
if (empty($_GET['language']) && (!isset($_SESSION['language']) || empty($_SESSION['language'])))
{
$dil = IP_ULKE();
if (!empty($dil))
$_SESSION['language'] = $user_info['language'] = $dil;
}

// If language is detected before, dont do it and dont load the server again..
if (isset($_SESSION['language']) && !empty($_SESSION['language']))
$user_info['language'] = $_SESSION['language'];

Home of Elmacik

Sheepy

I recall there's a member map mod with a visitor country ip flag extension that comes with a ip->country mapping database (that you can update manually in phpmyadmin by importing a cvs).  Would that be easier for the server?

Elmacik

Using the MySQL db? I dont know, maybe.. :)
Maybe I recall wrong but I think that mod was using files too... ? :-\
Mine was just an idea, and you will have to upload the files only and wont have to mess up with database. (at least for common users, not for advanced ;) )
Home of Elmacik

Thantos

You are assuming that the user speaks a particular language just because they are coming from a particular country which may not be true.  A current example would be the Olympics.  Just because someone logged in from there doesn't mean they speak Italian

Elmacik

You are right. But all the forums are not visited worldwide :)
For example my board; I have two languages installed. When someone browses from Turkey, the forum opens in Turkish. When its out of Turkey, then the forum opens in English.
So people can use for the same purpose, we dont have to install all the languages ;)
This is just the idea.
Home of Elmacik

Advertisement: