1

I'd like to which way is the best way to guess what is the user's language without asking him. Here are a couple of options that I've found, but feel free to add any other that you think is worth mentioning.

  1. Use $_SERVER['HTTP_ACCEPT_LANGUAGE'] via PHP
  2. Use any Maps API (such as Google Maps or Bing Maps) to determine what country the user is accessing the website from (if he is using a modern browser that support location services) and then detect what is that country's official language.
  3. Have PHP execute a simple shell script (shell_exec()) that send a request to a website like (http://geoip.s12.com.br or similar) to determine what country the user is accessing the website from and then detect what is that country's official language.
  4. I may be wrong about this one because I didn't make a lot of research about it: Use php's GeoIp function to determine what country the user is accessing the website from and then detect what is that country's official language. (http://www.php.net/manual/en/book.geoip.php)

I am aware that the first one will do in almost any case, however I'd like to know which one is more accurate (the way Facebook or Google use to set your language).

joaop
  • 75
  • 1
  • 12
  • Ask them. You could use the GEOIP but it will only tell you country or continent, and in a country there may be dozens or hundreds of languages and dialects. You really need to just ask or make some assumptions. – Rottingham Sep 09 '13 at 22:17
  • Google uses the accept language header to determine language preference. This also makes most sense, it works better than geolocation via IPs which is not so well for languages. As this has been discussed earlier please use the search and refer to the existing material when you ask about the same topic. – hakre Sep 09 '13 at 22:19
  • Further ref: [What's the best way to determine users' preferred language?](http://webmasters.stackexchange.com/q/23005/4965) and [Patterns for creating a Language Switcher?](http://webmasters.stackexchange.com/q/52363/4965) - what you ask about is more webmastering material than programming IMHO. – hakre Sep 09 '13 at 22:25

2 Answers2

2

I would go with $_SERVER['HTTP_ACCEPT_LANGUAGE'] mainly due to the fact that it is user configurable.

The issue with geo locating, is that language really shouldn't be tied to a location search. Sure, it would allow you to make an educated guess, but I could easily be vacationing in China, or prefer my internet to be in "language x"

Regardless of the method you use for your educated guess, I would definitely still provide the option to choose their preferred language to override your guess.

theBarkman
  • 131
  • 4
0

There are countries with several languages so 2,3 and 4 won't work at all, so for me, the best opion is to use HTT_ACCEPT_LANGUAGE.

Pep Lainez
  • 949
  • 7
  • 12