Reputation: 397
I am building a multilingual website. Is it possible to check if a particular language is installed/supported on the user's machine using PHP or Javascript? I want to detect this and display a message to the user if the language is not supported/installed.
Thanks, Mark.
Upvotes: 1
Views: 1583
Reputation: 3948
I've been doing multilingual stuff for the last ten years and my impression is that you're trying to overdo it: you're looking at it the wrong way. You shouldn't care what language (either for the OS or the browser) is installed on the user's machine: OS language doesn't matter, and modern browsers support practically all languages around (and, since the demise of NN4, all the writing systems/alphabets that found a place in the Unicode standard). What you should pursue is what language does the user prefers. The user is the bottleneck regarding the number of 'supported' languages.
The Accept-Language header could be a good solution - if it worked. To my experience, it does not - much too often. The average user of today still has to learn that the URL goes into the address bar and not into his homepage search form - I wouldn't expect more than 1-2% of the Net population to know about HTTP headers (yes, I'm in an optimistic mood tonight). Users will happily come to your website with whatever language their browser has been originally set up to claim. This includes tourists from cybercafès around the world, expatriates working abroad for companies with strong "don't mess with the config" policies, old folks whase IT-capable grandsons have a bizzarre sense of humour, shared machines and so on. You're going to guess wrong more often than not.
The concept of displaying a message for unsupported languages is dubious too. If the user with a, say, Punjabi browser is able to navigate to your site he would probably be able to see you don't supply Punjabi on its own - no need to interrupt his browsing with messages.
All that said, sometimes the simplest solution is the best: I'd suggest you let the user choose. Put out the list of available languages and the user will be more than happy to take the guesswork out of your code. And he/she will get it right every time.
Upvotes: 0
Reputation: 401002
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 :
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...
Upvotes: 6
Reputation: 2115
It is possible to learn the user's language preferences by examining the Accept header. <selfpromotion type="shameless"> I wrote a paper on the subject quite a few years ago (2000, phew!) exploring this and a couple of alternate mechanisms.</selfpromotion>
Quite a few interesting resources have came up since; particularly, I like this article, which takes a pragmatic approach for PHP.
Good luck!
Upvotes: 0
Reputation: 272106
Browsers typically send a Accept-Language header which might provide you a little hint about the language the person expects to see. In my case, my browser sent this header to stackoverflow.com:
Accept-Language: en-us,en;q=0.5
Assuming that you're using PHP on server side, you can lookup this information in the $_SERVER variable:
$_SERVER["HTTP_ACCEPT_LANGUAGE"]
Upvotes: 2