<?php function textreplace_transform_files($tr_table_file_or_xml,$textfiles=array()) { clearstatcache(); $tr_table_xml=null; $trtf_time=0; if (is_string($tr_table_file_or_xml) && is_file($tr_table_file_or_xml)) $trtf_time=filemtime($tr_table_file_or_xml); foreach ($textfiles as $tf) { $dest_labels=array(); $savable=true; if (is_file($tf['src'])) { $src_time=max($trtf_time,@filemtime($tf['src'])); $dest_time=0+@filemtime($tf['dest']); if ($dest_time<$src_time) { if (!$tr_table_xml) { if ($trtf_time) $tr_table_xml=@simplexml_load_file($tr_table_file_or_xml); if ($tr_table_file_or_xml instanceof SimpleXMLElement) $tr_table_xml=$tr_table_file_or_xml; } if ($tr_table_xml) { $src_labels=TemplateLoader::loadLabelFromFile($tf['src']); $dest_labels=textreplace_transform_labels($tr_table_xml,$src_labels); } } else $savable=false; } if ($savable) textreplace_save_labels($dest_labels,$tf['dest']); } } function textreplace_transform_labels($tr_table_xml,$src_labels) { $dest_labels=array(); foreach ($src_labels as $key=>$text) { foreach ($tr_table_xml->rule as $rule) $text=str_replace($rule->from,$rule->to,$text); $dest_labels[$key]=$text; } return $dest_labels; } function textreplace_save_labels($dest_labels,$dest_file,$line_sep="\n") { $dir=dirname($dest_file); @mkdir($dir,0777,true); $fp=fopen($dest_file,"w"); if (!$fp) return false; foreach ($dest_labels as $key=>$text) { if (strpbrk($text,"\r\n")) { $eot="EOT"; while (strpos($text,$eot)!==false) $eot.=rand(0,9); fwrite($fp,$key."=<<<".$eot.$line_sep.$text.$line_sep.$eot.$line_sep); } else fwrite($fp,$key."=".$text.$line_sep); } fclose($fp); return true; } ?>