Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 426
Steven Avatar asked Feb 23 '10 05:02

Steven


1 Answers

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. :)).

like image 189
deceze Avatar answered Oct 06 '22 01:10

deceze