Detecting which languages are installed might not be quite possible...
... But you can detect which languages the user is willing to get from websites -- ie, generally, which languages he can understand.
Most browser send an HTTP Header called "Accept-Language
", which can have a value such as "fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
" (this is what my current browser send to websites I am visiting)
On the PHP side, you can get this in the $_SERVER
array :
var_dump($_SERVER['HTTP_ACCEPT_LANGUAGE']);
Will output :
string 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' (length=35)
Here, it indicates I want websites to send me content in french ; but that I also accept english (preferably US english, but I'm OK with just plain default english too)
You can find quite a lot of examples of how to parse this in PHP ; for instance : Parse Accept-Language to detect a user's language.
Using the code provided in that article, I get this array of languages :
array
'fr' => int 1
'fr-fr' => string '0.8' (length=3)
'en-us' => string '0.5' (length=3)
'en' => string '0.3' (length=3)
ie :
- I prefer french
- if "fr" is not possible, I like "french from France"
- if that is not possible either, I accept "english from the US"
- and if that is not possible too, I'm qite OK with "english"
And if the website cannot serve any of those... Well, I suppose I don't have much of a choice, and will get whatever it wants to send me...