]> The preferred language a user accesses a web page with, determined using PHP

The preferred language a user accesses a web page with, determined using PHP


There are a lot of PHP codes that map the contents of $_SERVER['HTTP_ACCEPT_LANGUAGE'] onto a simple language string. Some of them do not work at all, some of them work in some cases, some of them work in many cases, and, there is probably no always working one. Here is my idea how that problem can be tackled. Please, let me know if there is any issue.


If Cut/Copy and Paste fails, then click here for download.


<?php

# Finds out the preferred language the user accesses the web page with.
# This function was written by Stephan K.H. Seidl in 2015, no copyright is claimed.
# It is offered as-is, without any warranty.
# This function is in the public domain; do with it what you wish.

function getbrowserlang ($allowedlangarray, $defaultlang)
{
  $browserlangspec = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  if (empty ($browserlangspec)) {
    return $defaultlang;
    }
  $browserlangspec = strtolower ($browserlangspec);
  $browserlangspec = preg_replace ('/\s+/', '', $browserlangspec);
  $browserlangspecitemarray = explode (',', $browserlangspec, 200);
  $lang = $defaultlang;
  $quality = 0;
  for ($i = 0; $i < count ($browserlangspecitemarray); $i++) {
    $browserlangspecitem = $browserlangspecitemarray[$i];
    if (strpos ($browserlangspecitem, ';q=') === FALSE) {
      $browserlangspecitem = sprintf ('%s;q=1', $browserlangspecitem);
      }
    $posrightmostsemicolon = strrpos ($browserlangspecitem, ';');
    $qualityasstring = substr ($browserlangspecitem, $posrightmostsemicolon);
    $qualityasnumber = -1;
    if (sscanf ($qualityasstring, ';q=%f', $qualityasnumber) !== 1) {
      $qualityasnumber = -1;
      }
    if ($qualityasnumber <= $quality || 1.000001 < $qualityasnumber) {
      continue;
      }
    $browserlangspecitem = substr ($browserlangspecitem, 0, $posrightmostsemicolon);
    $posleftmostminussign = strpos ($browserlangspecitem, '-');
    if ($posleftmostminussign !== FALSE) {
      $browserlangspecitem = substr ($browserlangspecitem, 0, $posleftmostminussign);
      }
    $langisallowed = 0;
    for ($j = 0; $j < count ($allowedlangarray); $j++) {
      if (strcmp ($allowedlangarray[$j], $browserlangspecitem) === 0) {
        $langisallowed = 1;
        break;
        }
      }
    if ($langisallowed === 0) {
      continue;
      }
    $lang = $browserlangspecitem;
    $quality = $qualityasnumber;
    }
  return $lang;
  }

header ('Content-type:application/xhtml+xml');

$lang = getbrowserlang (array ('de', 'en', 'es'), 'en');

$mydir = dirname (__FILE__);
$myfile = sprintf ("%s/%s/tlf.xhtml", $mydir, $lang);
$myfilecontents = file_get_contents ($myfile);

#$myneedle = '<!--#config timefmt="%Y" --><!--#echo var="DATE_LOCAL" -->';
#$myreplacement = date ('Y');
#$myfilecontents = str_replace ($myneedle, $myreplacement, $myfilecontents);

echo $myfilecontents;

?>

That is it.


Sun, 29 Nov 2015 12:00:00 +0100

Stephan K.H. Seidl