How to decide user's language

你离开我真会死。 提交于 2019-12-08 07:48:44

问题


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?


回答1:


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



回答2:


  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.




回答3:


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.




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/8435989/how-to-decide-users-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!