How to decide user's language

1.1k views Asked by At

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?

5

There are 5 answers

1
Tim On

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

6
Xeoncross On

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
0
Pekka On
  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.

0
mdo On

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.

0
rtacconi On

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.