<?php

//liste le dossiers des locales
function list_dir($dir) {
	if ($handle = opendir($dir)) {
		$files = array();
		while(false!==($file = readdir($handle))) {
			if (is_dir($dir . '/' . $file)) {
				$files[] = $file;
				}
			}
		closedir($handle);
		sort($files);
		$i = 0;
		global $myLanguages;
		$myLanguages = array();
		foreach($files as $f) {
			if (strstr($f,'.') == false) {
				$myLanguages[$i] = $f;
				$i++;
				}
			//$myLanguages[] = $f;
			}
		}
	else {
		echo 'error: missing language files';
		exit;
		}
	//print_r($myLanguages);
	return $myLanguages;
	}

function recherche($tableau, $string) {
	foreach ($tableau as $cle => $valeur) {
		if ($string == $cle) {
			$a = $valeur;
			break;
			}
		}
	return $a;
	}
	
function locale_language_from_browser($languages) {
  // Specified by the user via the browser's Accept Language setting
  // Samples: "hu, en-us;q=0.66, en;q=0.33", "hu,en-us;q=0.5"
  $browser_langs = array();

  if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    $browser_accept = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
    foreach ($browser_accept as $langpart) {
      // The language part is either a code or a code with a quality.
      // We cannot do anything with a * code, so it is skipped.
      // If the quality is missing, it is assumed to be 1 according to the RFC.
      if (preg_match("!([a-z-]+)(;q=([0-9\\.]+))?!", trim($langpart), $found)) {
        $browser_langs[$found[1]] = (isset($found[3]) ? (float) $found[3] : 1.0);
      }
    }
  }

  // Order the codes by quality
  arsort($browser_langs);
	//Array ( [en-us] => 1 [en] => 0.7 [fr] => 0.3 ) Firefox Fr
	//Array ( [fr-] => 1 [en-] => 1 [fr] => 0.8 [en] => 0.4 ) Chrome 5 Fr
	//Array ( [fr-fr] => 1 ) Safari Fr
	//Array ( [fr-fr] => 1 ) iCab Fr
	//Array ( [zh-] => 1 [fr] => 1 [pt-] => 1 [en-] => 1 [en] => 0.9 [ja] => 0.7 [de] => 0.6 [es] => 0.5 [it] => 0.4 [pt] => 0.3 [pl] => 0.1 [ru] => 0.1 [ko] => 0.1 [sv] => 0.1 [nl] => 0.1 [nb] => 0.1 [da] => 0.1 [fi] => 0.1 ) Opera 10.6

  // Try to find the first preferred language we have
  foreach ($browser_langs as $langcode => $q) {
  	foreach ($languages as $value) {
  	/*
  	$string = strtolower(str_replace('_','-',$value));
  	echo $langcode . '-' . $q . '-' . $value . '<br>';  	
	if ($string == $langcode) {
		echo $value;
		return $value;
		break;
		}
  	*/
  	//echo $langcode . '-' . $q . '-' . $value . '<br>';  	
	if (substr($langcode, 0, 2) == substr($value, 0, 2)) {
		$lang = $value;
		break 2;
		}
    }
  }

	if (!isset($lang)) $lang = 'fr_FR';
  return $lang;
}

// Affiche sur popup (POST=-1) ou une liste de liens (GET=-1) pour choisir la langue
//echo choose_lang($liste_locale, $get=0, $post=-1);
function choose_lang($select_language,$liste, $get, $post) {
	global $nation;
	if ($get == true) {
		$i = 1;
   		foreach ($liste as $value) {
	   		//echo "<a href = '" . $_SERVER['PHP_SELF'] ."?lang=" . $value . "'>" . recherche($nation,$value) . "</a>";
			$links .= "<a href = '" . $_SERVER['PHP_SELF'] ."?lang=" . $value . "'><img src='../images/flags/" . strtolower(substr($value, 3, 2)) . ".png' class='' width='18' height='12' alt='".gettext("flag")."' /></a>";
			if ($i < count($liste)) $links .=  ' | ';
			$i++;
			}
		}
	elseif ($post == true) {
		$links = "<form name='language' action=" . $_SERVER['PHP_SELF'] . " method='post'>\n";
		$links .= "<select onchange = 'document.language.submit()' name='lang'>\n";
		$links .= "<option selected='selected'>" . gettext($select_language) ."</option>\n";
		foreach ($liste as $value) {
    		$links .= "<option value='" . $value . "'>" . recherche($nation,$value) . "</option>\n";
    		}
		$links .= "</select>\n";
		$links .= "</form>\n";
		}
	return $links;	
	}
	

?>
