1

Topic: Language auto-detection

##
##
##        Mod title:  Language Auto-Detection
##
##      Mod version:  0.9
##   Works on PunBB:  1.0.1
##     Release date:  2003-08-23
##           Author:  Samuel Lidén Borell (samuellb@bredband.net)
##
##      Description:  Auto-detects language using ACCEPT_LANGUAGE http header.
##
##   Affected files:  config.php
##
##       Affects DB:  No
##
##            Notes:  This mod works on almost all versions of PunBB.
##
##                    Generated with ModGenerator (http://modgen.cactuz.nu/)
##                    on: 2003-08-24 11:23:49
##
##       DISCLAIMER:  Please note that "mods" are not officially supported by
##                    PunBB. Installation of this modification is done at your
##                    own risk. Backup your forum database and any and all
##                    applicable files before proceeding.
##
##


#
#---------[ 1. OPEN ]---------------------------------------------------------
#

config.php


#
#---------[ 2. FIND (line: 15) ]----------------------------------------------
#

$language = 'en';


#
#---------[ 3. AFTER, ADD ]---------------------------------------------------
#

$lng = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$LNG = explode(";", $lng);
$LNG = explode(",", $LNG[0]);
foreach ($LNG as $lang) {
  trim(strtolower($lang));
  $lng = explode("-", $lang);
  if (file_exists("lang/$lng[0]/index.html")) {
    $language = $lng[0];
    break;
  }
}


#
#---------[ 4. SAVE/UPLOAD ]--------------------------------------------------
#

Re: Language auto-detection

Cool! I'm not completely sure what this will do to the search indexing though (since it's locale specific). Oh, well, it'll probably work fine in most cases :)

"Programming is like sex: one mistake and you have to support it for the rest of your life."

Re: Language auto-detection

[topic cleaned]

"Programming is like sex: one mistake and you have to support it for the rest of your life."

4

Re: Language auto-detection

function get_pref_language_array($str_http_languages)
{
  $langs = explode(',',$str_http_languages);
  $qcandidat = 0;
  $nblang = count($langs);

  for ($i=0; $i<$nblang; $i++)
  {
    for ($j=0; $j<count($langs); $j++) {
      $lang = trim($langs[$j]); // Supprime les espaces avant et après $lang
      // Lang est de la forme langue;q=valeur
     
      if (!strstr($lang, ';') && $qcandidat != 1) {
        // Si la chaine ne contient pas de valeur de préférence q
        $candidat = $lang;
        $qcandidat = 1;
        $indicecandidat = $j;
      }
      else {
        // On récupère l'indice q
        $q = ereg_replace('.*;q=(.*)', '\\1', $lang);

        if ($q > $qcandidat) {
          $candidat = ereg_replace('(.*);.*', '\\1', $lang); ;
          $qcandidat = $q;
          $indicecandidat = $j;     
        }
      }
    }
   
    $resultat[$i] = $candidat;

    $qcandidat=0;
    // On supprime la valeur du tableau
    unset($langs[$indicecandidat]);   
    $langs = array_values($langs);
  }
  return $resultat;
}

I wrote that function once to detect the language, it's a bit more rfc compliant in that if I anderstand the above code correctly, you assume the first language given is the prefered one, wich is in most case true, but it's not defined that precisely in the RFC.
This function takes  $_SERVER['HTTP_ACCEPT_LANGUAGE'] as parameter, and returns an array containg the languages, beginning with the prefered one, and sorted by preference.

Hope this helps.

5

Re: Language auto-detection

Hello,

What I am looking for is the following :

For guest (and only guest), if browser is configurated with english, then forum is english. If the guest's browser is russian, then forum environment is russian.

Is there any one that could help me trying to install this on my forum ?

It seems to me that posted code in this thread does not look up to date any more.

I posted in http://www.punbb.fr/forums/viewtopic.php?id=3062 , but it seems the french are not up to do it.

Any one else ?

Thanks in advance.