Simplest way to detect client locale in PHP
Not guaranteed, but most browsers submit an Accept-Language HTTP header that specifies en-us if they're from the US. Some older browsers only said they are en, though. And not all machines are set up correctly to indicate which locale they prefer. But it's a good first guess.
English-UK based-users usually set their system or user locale to English-UK, which in default browser configurations should result in en-gb as the Accept Language header. (An earlier version of this said en-uk; that was a typo, sorry.) Other countries also have en locales, such as en-za (south africa), and, primarily theoretically, combinations like en-jp are also possible.
Geo-IP based guesses will less likely be correct on the preferred language/locale, however. Google thinks that content-negotiation based on IP address geolocation makes sense, which really annoys me when I'm in Japan or Korea...
Detect Browser Language in PHP
why dont you keep it simple and clean
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$acceptLang = ['fr', 'it', 'en'];
$lang = in_array($lang, $acceptLang) ? $lang : 'en';
require_once "index_{$lang}.php";
?>
PHP- Get current locale
You can call setlocale like so, and it'll return the current local.
$currentLocale = setlocale(LC_ALL, 0);
echo $currentLocale; //outputs C/en_US.UTF-8/C/C/C/C on my machine
Here is documentation from php.net as commented by @JROB
locale
If locale is "0", the locale setting is not affected, only the current setting is returned.
If locale is NULL or the empty string "", the locale names will be set from the values of environment variables with the same names as the above categories, or from "LANG".
If locale is an array or followed by additional parameters then each array element or parameter is tried to be set as new locale until success. This is useful if a locale is known under different names on different systems or for providing a fallback for a possibly not available locale.
PHP Detect User language
I'm not going to repeat all the valid answers here, but this link shows another good approach (for multi-lingo websites) taking advantage of http_negotiate_language() method
mirror
So combining that mapping and your exciting code you will have:
$map = array("en" => "english", "es" => "spanish");
$userLanguage = $map[ http_negotiate_language(array_keys($map)) ];
if ($userLanguage === 'english') {
echo "we have detected you are English. would you like to visit our site in English?";
} else {
header('location: /index.php?lang=default');
}
But if you are only interested to detect the english (en) language, you might want to shorten the script to:
if ('en' == substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)) {
echo "we have detected you are English. would you like to visit our site in English?";
} else {
header('location: /index.php?lang=default');
}
Detect web browser language using PHP
Based on your example, it seems that you're not concerned with the language localisation (if it's British English or American English for example). Rather you're only concerned that the language preference is English.
That being the case, I would suggest taking only the first two characters from the locale.
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
This will return you en
, fr
, ar
, etc, irrespective of the localisation. Use this to set the content language that is returned to the user, and if a language doesn't exist, server a default language, e.g. English.
Getting a user's language in PHP
As suggested I ended up falling back to English in an else statement if I cannot determine the language
Best way to determine user's language
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.
Related Topics
Create an Encrypted Zip Archive with PHP
Convert an Associative Array to a Simple Array of Its Values in PHP
Convert Month from Name to Number
String Contains Any Items in an Array (Case Insensitive)
How Get All Values in a Column Using PHP
Remove Index.Phproute=Common/Home from Opencart
Php: How to Disable Dangerous Functions
Remove Index.PHP in Codeigniter 2.1.0
Does PHP Execution Stop After a User Leaves the Page
How to Kill A/All PHP Sessions
Binding Params for Pdo Statement Inside a Loop
Many Hash Iterations: Append Salt Every Time
Access Denied for User 'Root'@'Localhost' with PHPmyadmin