2

I am looking for the best way to decided web user's language so that content can be presented in his native language. I want to know about pros and cons of different techniques.

Few options I am looking at are:

  • Using PHP geoIP extension which uses Maxminds database (free version)
  • Accessing user language from browser using http_negotiate_language

As I mentioned I don't want to dive deeper about the states and cities. I just want switch content based on users location/language.

Do you have any other suggestion?

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Gary Lindahl
  • 5,341
  • 2
  • 19
  • 18
  • I've tagged this as php, but you could also use the javascript navigator.language depending on where you need to know. Languages don't change crisply at borders, I'm sure many people in say, the US, browse with a preferred language of Spanish or Chinese, so your browser saying you live in New York, doesn't really tell me what language you speak where as the requests headers do say how the browser is configured (to prefer one language or is localized) – MatthewMartin Dec 08 '11 at 19:06

5 Answers5

3

Taken from the MicroMVC framework. The following looks for a cookie that could be set by a Javascript language selector - and if it's not found it uses the setting the users browser sends.

// Get locale from user agent
if(isset($_COOKIE['lang']))
{
    $preference = $_COOKIE['lang'];
}
else
{
    $preference = Locale::acceptFromHttp(getenv('HTTP_ACCEPT_LANGUAGE'));
}

// Match preferred language to those available, defaulting to generic English
$locale = Locale::lookup(config()->languages, $preference, false, 'en');

// Default Locale
Locale::setDefault($locale);
setlocale(LC_ALL, $locale . '.utf-8');
//putenv("LC_ALL", $locale);

Requires PHP 5.3 and the ICU INTL library.

$ sudo apt-get install php5-intl
Xeoncross
  • 55,620
  • 80
  • 262
  • 364
  • Using cookies has just the one big flaw that you can't open multiple pages from different languages simultaneously, which is why I tend to prefer a URL-based approach – Pekka Dec 08 '11 at 19:07
  • @Pekka, true, but don't forget you can limit cookies to certain paths if needed. – Xeoncross Dec 08 '11 at 19:10
  • @Pekka, what's the point having multiple pages open in different locales simultaneously? – idle sign Dec 12 '11 at 03:38
  • 1
    @idle comparing two translations... Doing a Google search for a specific term and finding it on two different language versions... There are many use cases, and it's terribly annoying to be forced into one version by the browser – Pekka Dec 12 '11 at 09:08
  • @Pekka There is no use for ordinary user to compare translations, all test&debug use cases are not really real user use cases. There is an issue with search engines though, I have to admit. – idle sign Dec 13 '11 at 07:23
  • 1
    An important note, the acceptFromHttp will only pull the first/highest value. If you support english by default and spanish, and a user prefers portuguese first, and spanish second. acceptFromHttp will only return portuguese, which you don't support, so you will end up with english even though you could have provided spanish. I'm looking into implementing this: http://www.thefutureoftheweb.com/blog/use-accept-language-header – Dustin Graham Oct 10 '13 at 16:29
1
  1. Use the browser's language

  2. Provide an easily accessible menu to switch languages at all times.

Using Geolocation to set the first choice is an option as well, but it's not 100% reliable, and the actual browser language is a much better indicator of what language the user speaks.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

Geo location will fail in many situations. For example an english speaking person on travel/vacation gets the wrong language presented while staying in Ukraine. I think most users have their correct language in the browser settings.

mdo
  • 6,961
  • 3
  • 24
  • 26
1

Get the browser's used language ($_SERVER['HTTP_USER_AGENT']) automatically but the provide a select box (or something else) to give the possibility to the user to select another language. If you are using a framework (and you should) it should have some helper method.

I would not use a GeoIP, say an US guy is browsing your site on holiday in France.

rtacconi
  • 14,317
  • 20
  • 66
  • 84
0

The best practice is to let your user decide their language from a selector.

Tim
  • 4,051
  • 10
  • 36
  • 60
  • My example above does this. First you ask the browser (which they already configured when they installed it), if that isn't good enough then let them select a language from the dropdown and save a cookie called "lang" to use that. – Xeoncross Dec 13 '11 at 15:50