1

Topic: Detect the language of anonymous users

In my quest to Detect the language of anonymous users (see :
http://punbb.org/forums/viewtopic.php?id=2534 )

I found this on http://wiki.punres.org/Detect_the_langu … mous_users

add this function to include/functions.php

// use User-Agent language to determine language for guests
// arg is associative array, sets ['language'], uses ['g_id']
// Mod by Han-Kwang Nienhuys, 2006-2007
function set_guest_lang(&$pun_user)
{
  if ($pun_user['g_id'] != PUN_GUEST)
    return;
  if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
    return;
  $acclang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  $lang = $pun_user['language'];
  if (preg_match('/sv.+nl/', $acclang)) { // prefer sv above nl
    $lang = 'Svenska';
  } else if (preg_match('/nl.+sv/', $acclang)) { // prefer nl above sv
    $lang = 'Nederlands';
  } else if (preg_match('/sv/', $acclang)) { // knows sv, not nl
    $lang = 'Svenska';
  } else if (preg_match('/nl/', $acclang)) { // knows nl, not sv
    $lang = 'Nederlands';
  }
  $pun_user['language'] = $lang;
}

And then find this in include/common.php :
// Check/update/set cookie and fetch user info
$pun_user = array();
check_cookie($pun_user) ; and add this below it:

set_guest_lang($pun_user);

As I just want the laguage to be detected, I tried to modify the code to :

// use User-Agent language to determine language for guests
// arg is associative array, sets ['language'], uses ['g_id']
// Mod by Han-Kwang Nienhuys, 2006-2007
function set_guest_lang(&$pun_user)
{
  if ($pun_user['g_id'] != PUN_GUEST)
    return;
  if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
    return;
  $acclang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  $lang = $pun_user['language'];
if (preg_match('/fr/', $acclang)) { // knows fr
    $lang = 'French';
  } else if (preg_match('/it/', $acclang)) { // knows en
    $lang = 'Italian';
  } else if (preg_match('/de/', $acclang)) { // knows de
    $lang = 'German';
  } else if (preg_match('/sp/', $acclang)) { // knows sp
    $lang = 'Spanish';
  } else if (preg_match('/pe/', $acclang)) { // knows pe
    $lang = 'Portuguese';
  } else if (preg_match('/ru/', $acclang)) { // knows ru
    $lang = 'Russian';
  } else { //default
    $lang = 'English';
  }
  $pun_user['language'] = $lang;
}

But it doesn't work (I don't much about PHP), would someone please let me know what I have done wrong ?

I have all translations installed.

Thanks in advance.

2 (edited by tankl 2007-11-05 08:21)

Re: Detect the language of anonymous users

FYI, here is the working code :

function set_guest_lang(&$pun_user)
{
  if ($pun_user['g_id'] != PUN_GUEST)
    return;
  if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
    return;
  $acclang = substr(strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']),0,2);
  $lang = $pun_user['language'];
  if ($acclang == "fr") { // prefer fr 
    $lang = 'French';
  } else if ($acclang == "en") { // prefer en
    $lang = 'English';
  } else if ($acclang == "de") { // prefer de
    $lang = 'German';
  } else if ($acclang == "es") { // prefer es
    $lang = 'Spanish';
  } else if ($acclang == "pt") { // prefer pt
    $lang = 'Portuguese';
  } else if ($acclang == "ru") { // prefer ru
    $lang = 'Russian';
  } else if ($acclang == "it") { // prefer it
    $lang = 'Italian';
  } else if ($acclang == "ro") { // prefer ro
    $lang = 'Romanian';
  } else if ($acclang == "pl") { // prefer pl
    $lang = 'Polish';
  } else if ($acclang == "tr") { // prefer tr
    $lang = 'Turkish';
  } else { //default
    $lang = 'English';
  }  
  $pun_user['language'] = $lang;
}

Cheers