Search:
Tip: Please give your vote in at least one Picks Poll to enable search results. Thank you.
Search for phrase rather than keywords

What language? Detecting preferred language via browser

7th October 2015

Page: prev. | 1 | next

Straight outta…

Depending on the site and its contents, there are obviously occasions when knowing the preferred language of a visitor is helpful, so that perhaps text can be presented in that language, the visitor redirected to a version in that language or content switched depending on cultural variation. Indeed, it could be considered a foundation strand of the new buzz word “contextual web” experience (wn.com).

Server-side

It’s actually a fairly simple matter as most browsers actually set a language preference header which can be examined by a server-side script, with PHP using
$_SERVER['HTTP_ACCEPT_LANGUAGE'].

The $_SERVER array element contains the contents of the Accept-Language: header from the browser’s request in the form of a two letter country code, usually combined with another if there is a variation of the language spoke—e.g. en for English, or or en-us for the United States, es for Spanish, es-es for Spanish spoke in Spain or es-mx in Mexico, or es-co in Colombia—and a relative quality factor (q=), a measure of preference in the case where multiple acceptable languages are present in bilingual cultures.

You can check what your browser returns here: HTTP header viewer (pgl.yoyo.org).

Or… well…

And a full list of language identification codes is available here: Web browser language identification codes (metamodpro.com).

The $_SERVER array element can be examined in a server-side script and decisions made.

Line
  1. <?php
  2. if (isset ($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  3.   $language = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
  4. }else {
  5.   $language = 'None'; /* Possibly Paleolithic cave-dweller on the evolutionary way to or header just not set by browser. */
  6. }
  7. /* The $language variable now contains language identification code, e.g. en-gb, en-us, es-mx etc. A switch statement or similar could be used here to proceed based on. */
  8. //Get first preference
  9. if ($language != 'None') {
  10.   x = strpos ($language,',');
  11.   /* Note strpos is zero based (first character is 0 not 1) so if comma is third character it shall return 2 */
  12.   if (x === false) {
  13.     /* Make sure use 'identical' (===) operator not just 'equal' (==) as this can be problematic with booleans as '0' can be interpreted as 'false'.
    A comma (,) not found in string thus only one language preference set. Look for semi-colon (;) instead. */
  14.     x = strpos ($language,';');
  15.   }
  16.   if (x !== false) { //Using 'not identical' operator
  17.     switch (substr ($language,0,x)) {
  18.       case 'en':
  19.       /* Note: most browsers will actually return en-us as they are American made and no significant difference seen unless specifically set in the browsers options. For example, by default it will likely return en-US,en;q=0.8 with default English (United States) set and en,en-US;q=0.8 only if British English is specifically set. This is why HTTP_ACCEPT_LANGUAGE is only for detecting preferred language rather than actual local. */
  20.       // And do whatever action…
  21.       break;
  22.       case 'en-us':
  23.       // Do whatever action…
  24.       break;
  25.       //More case expressions etc.
  26.     }
  27.   }
  28. }else {
  29.   //Language preference header not present…
  30. }
  31. ?>

Client-side

Although certainly preferable to do so server-side so that descisions and redirections can be made it is possible to detect language client-side via Javascript, useful if using a free service such as Blogger or Wordpress without programable access server-side. However, this does not work with some older browser versions—but could if correct depreciated Javascript function understood is researched and used:

Line
  1. <script type="text/javascript">
  2.   $language = navigator.language;
  3. </script>

An identical switch statement with Javascript could then be used but, of course, the client-side nature may limit to presenting a clickable link for preferred language options unless a lot of re-pokeage with inner and/or outerHTML or such is undertaken.

Never forgetting the internet is truly global, I hope it may prove useful to you.

Page: prev. | 1 | next

Tip: Please give your vote in a Poll to enable Tags search results. Thank you.

Disclaimer:

Illustrations, paintings, and cartoons featuring caricatured celebrities are intended purely as parody and fantasised depictions often relating to a particular news story, and often parodying said story and the media and pop cultural representation of said celebrity as much as anything else. Who am I really satirising? Read more.

Privacy policy

No cookies, ad and tracker free. Read more.