views:

197

answers:

4

Hi,

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.

+2  A: 

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"]
Salman A
+5  A: 

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...

Pascal MARTIN
When would a server not be able to provide 'fr' but be able to provide 'fr-fr'? Wouldn't it make more sense for these to be other way around?
Alohci
@Alohci : hu... I suppose you are right ^^ (I don't remember configuring the current browser I'm using -- I suppose I did that a bit too fast)
Pascal MARTIN
Thanks for the detailed reponse Pascal - very helpful.
Mark Blades
You're welcome :-) Have fun !
Pascal MARTIN
A: 

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!

codehead
Thanks for the links - found them to be a good read :)
Mark Blades
A: 

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.

djn
Funnily enough I have drawn the same conclusion from my research since yesterday i.e. Accept-Language isn't reliable. The last thing I want it the customer seeing an "error" for no reason. For that reason, I am going to op out of the 'auto-detection' solution.Thanks for your advice and for sharing your point of view.
Mark Blades