<?php

	require_once '../session_init.php';
	require_once 'config.inc.php';
	require_once 'fonctions.inc.php';

	if ( isset($_GET["ok"] ))
	{
		$fisrt_line = false;
		$fh = fopen("../lang/9mfo-traduction.csv", "r") or die( __FILE__ ." ligne: " . __LINE__ . " Can't open csv");
		$fhIndex = fopen("../lang/lang.json", "w") or die( __FILE__ ." ligne: " . __LINE__ . " Can't open lang/lang.json");
		fwrite($fhIndex, '{');
		$first_line = true;
		$first_column_line = true;
		$first_object = true;
		while( $line = fgets($fh))
		{
			echo $line . EOLH;
			$first_column = true;
			$table = explode('|', $line);
			$i =0;
			foreach ( $table as $column )
			{

				$column = trim($column, " \n\r\t\0\"" );

				if ($first_line == true)
				{
					//echo "first_line =>";
					if ( $first_column_line == true )
					{
						//echo "first_column";
						$first_column_line = false;
					}else
					{

						$index = explode("/",$column);
						echo "opening ../lang/" . $index[0] . EOLH;
						$fhl[++$i] = fopen("../lang/" . $index[0], "wb") or die( __FILE__ ." ligne: " . __LINE__ . " can't open lang/" . $index[0]);
						fwrite($fhl[$i], '{');
						if ( $first_column == true )
						{
							$first_column = false;
						}else
						{
							fwrite($fhIndex, ",");
						}
						fwrite($fhIndex, '"' . $index[0] . '":"' .  $index[1] . '"');
					}
					echo EOLH;
				}else
				{
					if (!empty($fhIndex))
					{
						fwrite($fhIndex, '}');
						fclose($fhIndex);
						unset($fhIndex);
					}
					//echo "not first_line =>";
					if ( $first_column == true )
					{
						$first_column = false;
						$index = $column;
						//echo "first_column = ". $index . "=>";
					}else
					{
						$i++;
						if ($first_object == false)  // insère la virgule
						{
							//echo "not first_object =>";
							fwrite($fhl[$i], ", ");
						}
						$text = '"' . $index . '":"' . $column .'"';
						fwrite($fhl[$i], $text );  //écrit l'objet json
						echo "writing ***" . $text .'***' . EOLH;
						fflush($fhl[$i]);

					}
					echo EOLH;
				}
				//echo EOLH;
			}
			if ($first_line == false)
			{
				$first_object = false;
			}
			$first_line = false;
		}
		fclose($fh);
		foreach( $fhl as $fhx)
		{
			fwrite($fhx, '}');
			fclose($fhx);
		}
		$fh = fopen("../lang/lang.json","r") or die("Can't open ../lang/lang.json" . __FILE__ ." ligne: " . __LINE__);
		$str = fgets($fh);
		fclose($fh);
		$langArray = json_decode( $str, true);
		foreach ($langArray as $key => $value)
		{

			$lang = getLang($key, true);
			if ( $lang != false )
			{
				print $key ." : ";
				print json_last_error_msg() . EOLH;
			}else
			{
				print  __FILE__ ." ligne: " . __LINE__ . " erreur à l'ouverture du fichier";
			}
		}
	}
?>
