In order to use this script, you can rename it to "index.php" and replace the variables $base, $url_1 and $xx accordingly 🙂
<?php
/*Copyright (c) 2010, Jared Abbott
Name: GeoTranslate (G-Tran)
Author: Jared Abbott
Email: g.sys.is@gmail.com
Follow Me @ http://twitter.com/o0110o
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// This is the base name for a website.
$base = 'androidista.com';
// The first part of the URL that gets a visitors locale information; this gets prepended to $url_0.
define('QUERY_0', 'http://api.hostip.info/get_html.php?ip=');
// The first part of the translated destination URL; this gets prepended to $results_1.
define('QUERY_1', 'http://translate.googleusercontent.com/translate_c?hl=en&sl=en&tl=');
// The final part of the translated destination URL; this gets appended to $results_1.
define('SITE_0', '&u=http://' .$base. '/&rurl=translate.google.com&twu=1&usg=ALkJrhhwDyuf8cf6PuDSnhiPbd1mgpwmWQ');
function getTranslation() {
ob_start();
// The default language of a website.
$xx= 'en';
// The top-level URL for a website.
$url_1 = 'http://androidista.com/';
// Gets a visitors IP address; this gets appended to QUERY_0 in $url_0.
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{$ip = $_SERVER['HTTP_CLIENT_IP'];}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];}
else
{$ip = $_SERVER['REMOTE_ADDR'];}
// The full URL to get a visitors locale information.
$url_0 = QUERY_0.$ip;
// A request for a visitors locale information.
$ch_0 = curl_init();
curl_setopt($ch_0, CURLOPT_URL, $url_0);
curl_setopt($ch_0, CURLOPT_HEADER, 0);
curl_setopt($ch_0, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch_0, CURLOPT_FAILONERROR, TRUE);
$locale = curl_exec($ch_0);
curl_close($ch_0);
// Extracts the visitors country name from the value of $locale and saves the results as the value of $results_0.
if( preg_match('/Country: (.*) .*/', $locale, $matches_0) )
{$results_0 = $matches_0[1];}
else{$results_0 = 'getLocation failure';}
//The name of the visitiors country.
$country = $results_0;
// A seperator which is used in the $languages string.
$seperator = '0';
// Combines $country with $seperator.
$country = "$country$seperator";
// A string of all the recognized countries in the world and their most prominent written languages. Languages which belong the language pair family supported by Google all have a '1' appended to them.
$languages = 'Afghanistan0ps, Akrotiri0en1, Albania0sq1, Algeria0ar1, American Samoa0en1, Andorra0ca1, Angola0pt-PT1, Anguilla0en1, Antarctica0en1, Antigua and Barbuda0en1, Argentina0es1, Armenia0hy, Aruba0nl1, Ashmore and Cartier Islands0en1, Australia0en1, Austria0de1, Azerbaijan0az,
The Bahamas0en1, Bahrain0ar1, Bangladesh0bn, Barbados0en1, Bassas da India0fr1, Belarus0be1, Belgium0nl1, Belize0en1, Benin0fr1, Bermuda0en1, Bhutan0bo, Bolivia0es1, Bosnia and Herzegovina0hr1, Botswan>en, Bouvet Island0en1, Brazil0pt-PT1, British Indian Ocean Territory0en1, British Virgin Islands0en1, Brunei0ms1, Bulgaria0bg1, Burkina Faso0fr1,
Burma0my, Burundi0fr1, Cambodia0en1, Cameroon0en1, Canada0en1, Cape Verde0pt-PT1, Cayman Island0en1, Central African Republic0fr1, Chad0ar1, Chile0es1, China0zh-CN1, Christmas Island0en1, Clipperton Island0fr1, CocoIslands0en1, Colombia0es1, Comoros0ar1, Democratic Republic of the Congo0fr1, Republic of the Congo0fr1, Cook Islands0en1,
Coral Sea Islands0en1, Costa Rica0es1, Cote d\'Ivoire0fr,Croatia0hr1, Cuba0es1, Cyprus0el1, Czech Republic0cs1, Denmark0da1, Dhekelia0el1, Djibouti0ar1, Dominica0en1, Dominican Republic0es1, Ecuador0es1, Egypt0ar1, El Salvador0es1, Equatorial Guinea0es1, Eritrea0ar1, Estonia0et1, Ethiopia0en1, Europa Island0en1, Falkland Islands0en1, Faroe Islands0da1, Fiji0en1, Finland0fi1,
France0fr1, French Guiana0fr1, French Polynesia0fr1, French Southern and Antarctic Lands0fr1, Gabon0fr1, The Gambia0en1, GazStri>ar, Georgia0ka, Germany0de1, Ghana0en1, Gibraltar0en1, Glorioso Islands0fr1, Greece0el1, Greenland0en1, Grenada0en1, Guadeloupe0fr1, Guam0en1, Guatemala0es1, Guernsey0en1, Guinea0fr1, Guinea Bissau0pt-PT1, Guyana0en1, Haiti0fr1,
Heard Island and McDonald Islands0en1, Honduras0es1, Hong Kong0zh-CN1, Hungary0hu1, Iceland0is1, India0hi1, Indonesia0id1, Iran0fa1, Iraq0ar1, Ireland0en1, Isle of Man0en1, Israel0iw1, Italy0it1, Jamaica0en1, Jan Mayen0no1, Japan0ja1, Jersey0en1, Jordan0ar1, Juan de Nova Island0fr1, Kazakhstan0kk, Kenya0sw1, Kiribati0en1, North Korea0ko1, South Korea0ko1,
Kuwait0ar1, Kyrgyzstan0ky, Laos0lo, Latvia0lv1, Lebanon0ar1, Lesotho0en1, Liberia0en1, Libya0ar1, Liechtenstein0de1, Lithuania0lt1, Luxembourg0de1, Macau0pt-PT1, Macedonia0mk1, Madagascar0fr1, Malawi0en1, Malaysia0ms1, Maldives0dv, Mali0fr1, Malta0mt1,
Marshall Islands0en1, Martinique0fr1, Mauritania0ar1, Mauritius0fr1, Mayotte0fr1, Mexico0es1, Federated States of Micronesia0en1, Moldova0ro1, Monaco0fr1, Mongolia0mn, Montserrat0en1, Morocco0ar1, Mozambique0pt-PT1, Namibia0af1, Nauru0en1, Navassa Island0en1, Nepal0ne, Netherlands0nl1, Netherlands Antilles0nl1, New Caledonia0fr1, New Zealand0en1,
Nicaragua0es1, Niger0fr1, Nigeria0en1, Niue0en1, Norfolk Island0en1, Northern Mariana Islands0en1, Norway0no1, Oman0ar1, Pakistan0ur, Palau0en1, Panama0es1, Papua New Guinea0en1, Paracel Islands0vi1, Paraguay0es1, Peru0es1, Philippines0tl1, Pitcairn Islands0en1, Poland0pl1, Portugal0pt-PT1, Puerto Rico0es1,
Qatar0ar1, Reunion0fr1, Romania0ro1, Russia0ru1, Rwanda0en1, Saint Helena0en1, Saint Kitts and Nevis0en1, Saint Lucia0en1,
Saint Pierre and Miquelon0fr1, Saint Vincent and the Grenadines0en1, Samoa0en1, San Marino0it1, Sao Tome and Principe0pt-PT1, Saudi Arabia0ar1, Senegal0fr1, Serbia and Montenegro0sr1, Seychelles0fr1, Sierra Leone0en1, Singapore0en1, Slovakia0sk1, Slovenia0sl1, Solomon Islands0en1, Somalia0ar1, South Africa0af1, South Georgia and the South Sandwich Islands0en1,
Spain0es1, Spratly Islands0en1, Sri Lanka0si, Sudan0ar1, Suriname0nl1, Svalbard0no1, Swaziland0en1, Sweden0sv1, Switzerland0de1, Syria0ar1, Taiwan0zh-CN1, Tajikistan0tg, Tanzania0sw1,
Thailand0th1, Timor-Leste0pt-PT1, Togo0fr1, Tokelau0en1, Tonga0en1, Trinidad and Tobago0en1, Tromelin Island0en1, Tunisia0ar1, Turkey0tr1, Turkmenistan0ru1, Turks and Caicos Islands0en1, Tuvalu0en1, UNKNOWN0en1, Uganda0en1, Ukraine0uk1, United Arab Emirates0ar1, United Kingdom0en1,
United States0en1, Uruguay0es1, Uzbekistan0uz, Vanuatu0en1, Vatican City0it1, Venezuela0es1, Vietnam0vi1, Virgin Islands0en1, Wake Island0en1, Wallis and Futuna0fr1, West Bank0ar1, Western Sahara0ar1, Yemen0ar1, Zambia0en1, Zimbabwe0en1, ';
// Extracts a visitors language code from $languages; the language extracted depends on the value of $country as each country is paired with its own language code.
if(preg_match("/$language(.*)\1/i", $languages, $matches_1))
{$results_1 = $matches_1[1];}
else{$results_1 = 'getLanguage failure';}
// Removes any characters which are trailing the visitors language code.
$string = explode(', ', $results_1);
// If extracting the visitors language code fails, the websites default language code will be the value of $results_1; However, if extracting the visitors language code is a success, then the value of $string becomes the value of $results_1.
if(preg_match('/getLanguage failure/', $results_1))
{$results_1 = $xx;}
else{$results_1 = $string[0];}
// The full URL to the translated destination.
$url_2 = QUERY_1.$results_1.SITE_0;
// If the value of $results_1 contains the websites default language code, a translation does not take place and the visitor is redirected to url_1; if otherwise a translation will take place and the visitor is redirected to url_2.
if(preg_match("/$xx/", $results_1))
{header("Location: " . $url_1);}
else{header("Location: " . $url_2);}
ob_end_clean();
exit();
}
getTranslation();
?>
Any comments and examples are welcome. : )