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

8

6

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?

Vick answered 23/2, 2010 at 5:28 Comment(4)
I believe you are asking two things - how to assign an IP to one of your languages (or which country is the user from)? And how to display all messages in the app in that correct language? Is this right?Rhombus
Only three languages, English, Chinese and Japanese. If I know the visitor comes from Japan, I will redirect it to the Japanese version;if the visitor comes from the Greater China, I will redirect it to the Chinese version; else I will redirect it to the English version.Vick
it is very obvious you want to easiest way to do it .. there is no point in adding it to the question.Monoploid
Don't. Just because I'm from Belgium, Myspace and Mappy.com always serve me pages in French... I don't speak French. More than half of people in Belgium don't speak French.Caryloncaryn
P
27

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

Pralltriller answered 23/2, 2010 at 5:43 Comment(3)
+1 for always give the user the choice to switch to another language... this is a best way.. :)Brian
What if I really want to find the language spoken in an area based on IP address? Lets say I have such a requirement.Luggage
@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.Pralltriller
A
2

Check out GeoPlugin:

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

Allanadale answered 23/2, 2010 at 5:32 Comment(0)
I
2

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.

Id answered 23/2, 2010 at 5:48 Comment(1)
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).Pralltriller
B
2
<?php
$ln = split(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]);
print_r($ln[0]);
?>
Boomer answered 23/2, 2010 at 7:53 Comment(1)
For those who got here just now (like me), split() is now deprecated (PHP 5.3.0). explode() works fine.Hutcheson
C
0

Perhaps this will help: www.countryipblocks.net

Cuspidor answered 23/2, 2010 at 5:32 Comment(0)
F
0

take a look at the maxmind geoip module for PHP (http://www.maxmind.com/app/php), as for your data structure perhaps key it to the ISO-3166-1 country code which apache_note("GEOIP_COUNTRY_CODE"); returns.

Foundation answered 23/2, 2010 at 5:32 Comment(1)
A country code is not enough: countries like Belgium and Switzerland have more than one official language.Caryloncaryn
H
0

You'd probably want to use some form of IP geocoding database (example).

Hyssop answered 23/2, 2010 at 5:33 Comment(0)
R
0

Assuming you can convert IP ranges to one of your language choices, you could do this (all replies above): have all your messages in the applications stored in an associative array of this form.

$MESSAGES[$USER_LANGUAGE][$msgId]

where $USER_LANGUAGE can be chinese, japanese, or english (or any other equivalent enum). $msgId can be things like "login.successful", "login.fail" etc. Where ever you display messages to the user do not display hardcoded strings, make a reference to the variable using the $msgId.

You can access it as a global variable OR you can create a function that takes in the $msgId as a parameter and returns the message, $USER_LANGUAGE can be a global variable as well (which is set the first time the user comes in).

Rhombus answered 23/2, 2010 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.