dal=$dal; $this->al=$al; if (!$percorso) $percorso=dirname(__FILE__)."/langs/{$this->dal}-{$this->al}.diz.php"; if ("$dal-$al"!='it-it' && !isset($this->container["$dal-$al"])) {$traduzioni=&$this->containers["$dal-$al"]; @include($percorso); $this->percorso=realpath($percorso); } $this->container=&$this->containers["$dal-$al"]; } /*** @ignore **/ protected static function file_write($nome,$content) { $md5=md5($content); if ($md5==@md5_file($nome)) return true; $new_name=$nome.'.'.microtime(1).mt_rand(10,99999999).'.tmp'; do{if (@file_put_contents($new_name,$content) && $md5==@md5_file($new_name) && (!is_file($nome) || @rename($nome,"$new_name.prev")) && @rename($new_name,$nome)) {@unlink("$new_name.prev"); return true; } @unlink("$new_name.prev"); @unlink($new_name); usleep($wt=rand(100,10000)); @unlink("$new_name.prev"); @unlink($new_name); $n+=$wt; } while ($n<1000000); return false; } /** * Attiva/disattiva log errori * (in sostanza nell'array del file dizionario si aggiungono tutte le parole non trovate) * * @param boolean $stato */ function log_errori($stato=true) { $this->log=$stato; } /** * Restituisce l'array con tutte le traduzioni * * @return array (è un array associativo che ha come chiave il testo da tradurre e valore il testo tradotto) */ function get_trasl() { $v=$this->container; ksort($v); return $v; } /** * Restituisce il codice della lingua da cui si traduce * * @return string */ function get_dal() {return $this->dal;} /** * Restituisce il codice della lingua in cui si traduce * * @return string */ function get_al() {return $this->al;} /** * Aggiunge le traduzioni passate al dizionario corrente senza salvare * * @param array $traduzioni (è un array associativo che ha come chiave il testo da tradurre e valore il testo tradotto) */ function add_trasl($traduzioni) { foreach ($traduzioni as $da=>&$in) if (!$this->casesens) $this->container[strtolower($da)]=$in; else $this->container[$da]=$in; } /** * Sostituisce le traduzioni passate al dizionario corrente senza salvare * * @param array $traduzioni (è un array associativo che ha come chiave il testo da tradurre e valore il testo tradotto) */ function set_trasl($traduzioni) { $this->container=array(); $this->add_trasl($traduzioni); } /** * Salva le traduzioni passate nel dizionario corrente * * @param array $traduzioni (è un array associativo che ha come chiave il testo da tradurre e valore il testo tradotto) se omesso si risalvano i valori correnti * @param boolean $quota se true effettua quotatura dei testi */ function Salva($traduzioni='',$quota=true){ if (!$traduzioni) $traduzioni=$this->get_trasl(); $testo="&$ad) {if ($quota) {$da=str_replace("'","\\'",$da); $ad=str_replace("'","\\'",$ad); } $testo.="\$traduzioni['$da']='$ad';\n"; } $testo.="\n?>"; self::file_write($this->percorso,$testo); unset($this->container["{$this->dal}-{$this->al}"]); self::myDizionario($this->dal,$this->al,$this->percorso); } /** * Imposta il case per le traduzioni * * @param boolean $sensitive se true è sensitive se false è insensitive */ function set_case($sensitive) { $this->casesens=$sensitive; //if (!$sensitive && $this->container) foreach ($this->container as $da=>&$in) $this->container[strtolower($da)]=$in; } /** * Traduce un testo * * @param string $x testo da tradurre * @param array $parole array associativo per eventuali segnaposto * @param boolean $esito se si è trovata una traduzione per il testo * @return string * * * Supponendo che il file 'miei_dizionari/mie_voci_ing_it.inc' * contenga: * 'you have a game', * 'tu hai #n# giochi'=>'you have #n# games' * ); * ?> * * * Possiamo scrivere: * trasl('tu hai un gioco'); //se $n vale 1 usa una frase al singolare * else echo $d->trasl('tu hai #n# giochi',array('#n#'=>$n)); //altrimenti la usa al plurale parametrizzando il numero $n * ?> * */ function trasl($x,$parole=array(),&$esito='') { $esito=true; if ($this->dal=='it' && $this->al=='it') $nuova=$x; else{$esito=false; if (!($x=trim($x))) return $x; if ($this->casesens) { $esito=isset($this->container[$x]); $nuova=$this->container[$x]; } else{ $v=array_change_key_case($this->container); $esito=isset($v[strtolower($x)]); $nuova=$v[strtolower($x)]; } if (!$esito) { if ($this->log) $this->to_save=true; $this->container[$x]=''; } //if (!$nuova) $nuova=$x; } if ($nuova && $parole) $nuova=strtr($nuova,$parole); return $nuova; } function __destruct() { if ($this->to_save) {$this->salva(); if(is_callable('accelerator_reset')) accelerator_reset(); } } } ?>