How to detect a user's language through their IP Address

18,080

Solution 1

Please, PLEASE, do not make the mistake of thinking that IP == language. Look at the browsers accept-language header first, then the browser identification string which might contain the OS language, and only then take the IP into account. In almost 100% of all cases the browser accept-language header will be present and sufficient.

And always give the user the choice to switch to another language.

Just apart from the simple case of a foreigner abroad, how do you determine the language for Belgium, where they speak French, Dutch and German? (Maybe that doesn't apply to your case, but just philosophically. :)).

Solution 2

Check out GeoPlugin:

http://www.geoplugin.com/webservices/php

Solution 3

Yes please don't do it... Google does this and dreaking annoying.. I always get the thai version instead the english one from my browser.

Use the http headers from the browser.

Solution 4

<?php
$ln = split(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]);
print_r($ln[0]);
?>
Share:
18,080
Steven
Author by

Steven

I'm a headhunter based in Hangzhou, China. I recruit software engineers, language experts, C-suite talents for companies. If you need my headhunting services, you can contact me via steven.tu[at]mipfanyi.com(Please replace "[at]" with "@"). If you want to get a job, you can also send a resume to my email.

Updated on June 20, 2022

Comments

  • Steven
    Steven almost 2 years

    I have developed a website www.tenxian.com.

    It has three language versions, English, Japanese and Chinese. How can I write an effective PHP program which can automatically choose a language version based on the IP address of the visitor?

    If I use "if-else", the code would be much complicated; If I use switch-case, how to write it since the data which should be dealt with are IP ranges, not particular numbers. Besides, I don't know these IP ranges

    What is the easiest way to do it?

  • Gromski
    Gromski about 14 years
    Google is horrible in this regard. Even if you managed to switch the interface to a different language, it will revert back to your IPs locale when clicking through to a different service (like Google homepage -> Maps).
  • RameshVel
    RameshVel about 14 years
    +1 for always give the user the choice to switch to another language... this is a best way.. :)
  • bart
    bart about 14 years
    A country code is not enough: countries like Belgium and Switzerland have more than one official language.
  • Bruno Gomes
    Bruno Gomes over 11 years
    For those who got here just now (like me), split() is now deprecated (PHP 5.3.0). explode() works fine.
  • Ajay George
    Ajay George over 6 years
    What if I really want to find the language spoken in an area based on IP address? Lets say I have such a requirement.
  • Gromski
    Gromski over 6 years
    @Ajay Find a database of what languages are spoken where, geolocate the IP and smush the two together. Take a best guess if there's more than one resulting language.