Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

TranslationExportXaraya.php

Go to the documentation of this file.
00001 <?
00002 
00003 /***************************************************************************
00004  *   Copyright (C) 2005 by Ferenc Veres   *
00005  *   lion@netngine.hu   *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 
00030 class TranslationExportXaraya implements TranslationExport
00031 {
00032 
00033         private $RootTimeStamp; 
00034         private $SilentMode; 
00045         private function &ExportProject(TranslationProject $project)
00046         {
00047                 $langXml = new DOMDocument();
00048                 //$langXml->substituteEntities = true;
00049                 $langXml->LoadXML($project->GetSkeleton());
00050 
00051                 // Loop on all strings of this project and put in a hash for later replace
00052                 $strings = new StringIterator($project->GetID());
00053                 foreach($strings as $str)
00054                 {
00055                         $projStrings["###".$str->GetStringID()."###"] = $this->FixEntity($str->GetOriginal());
00056                         
00057                         $projStrings["###".$str->GetStringID()."t###"] = $this->FixEntity($str->GetTranslation());
00058 /*                      if(preg_match('/\&/', $str->GetOriginal()))
00059                         {
00060                                 echo( $str->GetOriginal()."\n");
00061                                 echo(FixQuot($str->GetOriginal()."\n"));
00062                         }
00063 */              }
00064 
00065                 // XPath to all translation entries
00066                 $xpath = new DOMXPath($langXml);
00067                 $xpath->registerNamespace("m","http://xaraya.com/2002/ns/translations");
00068                 $entryNodes = $xpath->query("//m:translations/m:entry");
00069 
00070                 // Loop on each string in theXML and replace variables
00071                 foreach ($entryNodes as $entry)
00072                 {
00073                         $origNodes = $xpath->query("m:string", $entry);
00074                         $transNodes = $xpath->query("m:translation", $entry);
00075 
00076                         if($origNodes->item(0)->nodeValue == "")
00077                         {
00078                                 echo "Empty Original node in the file:".$project->GetName().".\n";
00079                         }
00080                         else
00081                         {
00082                                 // Replace ### node with the same strings hash key value
00083                                 $origNodes->item(0)->nodeValue = $projStrings[$origNodes->item(0)->nodeValue];
00084                                 
00085                                 $transNodes->item(0)->nodeValue = $projStrings[$transNodes->item(0)->nodeValue];
00086 
00087                         }
00088                 }
00089                 $xmlContent =& $langXml->saveXML();
00090                 return $xmlContent;
00091         }
00092 
00101         function FixEntity($text)
00102         {
00103                 $s = str_replace("&", "&amp;", $text);
00104                 $s = str_replace("\"", "&quot;", $s); // !!!
00105                 $s = str_replace("<", "&lt;", $s);
00106                 $s = str_replace(">", "&gt;", $s);
00107                 return $s;
00108         }
00109 
00119         private function ProcessRecursive($dir, ProjectIterator $projectIterator)
00120         {
00121                 // Process each project in the iterator
00122                 foreach($projectIterator as $project)
00123                 {
00124                         // Completely ignore dead projects and their subprojects (older than root)
00125                         if($this->RootTimeStamp <= $project->GetLastSeen())
00126                         {
00127                                 // Empty document means direcrory only.
00128                                 if($project->GetDocument() == "")
00129                                 {
00130                                         // Create a new dir and process the connected projects recursively.
00131         
00132                                         $newDir = $dir."/".$project->GetName();
00133                                         if(!is_dir($newDir))
00134                                         {
00135                                                 $ret = mkdir($newDir);
00136                                                 if(!$ret)
00137                                                 {
00138                                                         throw new Exception("Failed to create $newDir, no permission or disk full.");
00139                                                 }
00140                                         }
00141                                         if($projectIterator->hasChildren())
00142                                         {
00143                                                 $this->ProcessRecursive($newDir, $projectIterator->getChildren());
00144                                         }
00145                                 }
00146                                 else
00147                                 {
00148                                         // Save one project XML file.
00149                                         
00150                                         $export = $this->ExportProject($project);
00151                                         $bytes = file_put_contents($dir."/".$project->GetName(), $export);
00152                                         if($bytes != strlen($export))
00153                                         {
00154                                                 throw new Exception("Failed to save  ".$dir."/".$project->GetName().", no permission or disk full.");
00155                                         }
00156                                         if(!$this->SilentMode)
00157                                         {
00158                                                 echo("Saved: ".$dir."/".$project->GetName()."\n");
00159                                         }
00160                                 }
00161                         }
00162                 }
00163 
00164         }
00165         
00175         public function Export($localeName, $silentMode = false)
00176         {
00177                 $this->SilentMode = $silentMode;
00178                 
00179                 $localeID = TranslationProject::$LocaleIDCache[$localeName];
00180                 if(empty($localeID))
00181                 {
00182                         throw new Exception("ExportXaraya called with unknown locale name: $localeName");
00183                 }
00184                 $dir = GetConfigVar('export_path')."/".$localeName."/xml/";
00185                 
00186                 $rootProject = new TranslationProject();
00187                 $rootProject->LoadRoot("Xaraya", $localeID);
00188                 $this->RootTimeStamp = $rootProject->GetLastSeen();
00189                 
00190                 $allProjects = new ProjectIterator($rootProject->GetID(), $localeID);
00191 
00192                 if(!is_dir($dir))
00193                 {
00194                         mkdir($dir, 0755, true);
00195                 }
00196                 $this->ProcessRecursive($dir, $allProjects);
00197         }
00198 }
00199 ?>

Generated on Sat Apr 22 16:49:53 2006 for XarayaTranslationMemory by  doxygen 1.4.4