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

TranslationImportXaraya.php

Go to the documentation of this file.
00001 <?
00002 /***************************************************************************
00003  *   Copyright (C) 2005 by Ferenc Veres   *
00004  *   lion@netngine.hu   *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  *   This program is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU General Public License for more details.                          *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU General Public License     *
00017  *   along with this program; if not, write to the                         *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00020  ***************************************************************************/
00021 include_once("lib/TranslationProject.php");
00022 
00043 class TranslationImportXaraya implements TranslationImport
00044 {
00045         private $LocaleID;
00046 
00051         function __construct()
00052         {
00053         }
00054         
00062         private function ImportFile($fullName, TranslationProject $thisProject)
00063         {
00064                 // Load the XML file
00065                 $langXml = new DOMDocument();
00066                 if(!$langXml->load($fullName))
00067                 {
00068                         throw new Exception("Can't load language XML file.\n");
00069                 }
00070 
00071                 // Store the document in the project
00072                 $document = $langXml->saveXML();
00073                 $thisProject->SetDocument($document);
00074                 
00075                 // XPath to all translation entries
00076                 $xpath = new DOMXPath($langXml);
00077                 $xpath->registerNamespace("m","http://xaraya.com/2002/ns/translations");
00078                 $entryNodes = $xpath->query("//m:translations/m:entry");
00079 
00080                 $projID = $thisProject->GetID();
00081                 $locID = $thisProject->GetLocaleID();
00082 
00083                 $rowCount = 0;
00084                 
00085                 // Loop on each string
00086                 foreach ($entryNodes as $entry)
00087                 {
00088                         $origNodes = $xpath->query("m:string", $entry);
00089                         $transNodes = $xpath->query("m:translation", $entry);
00090 
00091                         if($origNodes->item(0)->nodeValue == "")
00092                         {
00093                                 echo "Empty Original node in the current file.\n";
00094                         }
00095                         else
00096                         {
00097                                 // Stores translation and create index if not yet done
00098                                 $str = new TranslationString(
00099                                         0,
00100                                         $projID,
00101                                         $origNodes->item(0)->nodeValue, 
00102                                         $locID, 
00103                                         $transNodes->item(0)->nodeValue,
00104                                         0);
00105                                 $str->SetRowOrder($rowCount++);
00106                                 $id = $str->Save();
00107                                 
00108                                 $origNodes->item(0)->nodeValue = "###${id}###";
00109                                 $transNodes->item(0)->nodeValue = "###${id}t###";
00110 
00111                         }
00112                 }
00113 
00114                 // Store the finished skeleton in the project
00115                 $skeleton = $langXml->saveXML();
00116                 $thisProject->SetSkeleton($skeleton);
00117                 $thisProject->Save();
00118         }
00119 
00131         function ProcessDir(RecursiveDirectoryIterator $dirIterator, TranslationProject $parentProject)
00132         {
00133                 foreach($dirIterator as $file) {
00134 
00135                         // Create new subproject for ourselves or get existing one
00136                         $newProject = new TranslationProject();
00137                         $fname = preg_replace('/.*\//', '', $file->getFilename()); // FIXME: required due to a bug in php5.1.2
00138 
00139                         if(!$newProject->Load($parentProject->GetID(), $fname, $parentProject->GetLocaleID()))
00140                         {
00141                                 $newProject->CreateProject($parentProject->GetID(), $fname, $parentProject->GetLocaleID());
00142                         }
00143                         // Save new or update existing one's timestamp
00144                         $newProject->Save();
00145 
00146                         // Process files here
00147                         if($file->isFile() && preg_match("/\.xml$/", $fname))
00148                         {
00149                                 echo "process_file(".$file->getPathname().")\n";
00150                                 $this->ImportFile($file->getPathname(), $newProject);
00151                         }
00152 
00153                         // Go for subdirs
00154                         if($file->isDir()) {
00155                                 $this->ProcessDir($dirIterator->getChildren(), $newProject);
00156                         }
00157                 }
00158         }
00159 
00167         function Import($localeName)
00168         {
00169                 $localeID = TranslationProject::$LocaleIDCache[$localeName];
00170                 if(empty($localeID))
00171                 {
00172                         throw new Exception("ExportXaraya called with unknown locale name: $localeName");
00173                 }
00174                 
00175                 $dir = GetConfigVar('import_path')."/".$localeName."/xml/";
00176                 if(!is_dir($dir))
00177                 {
00178                         throw new Exception("ExportXaraya using nonexisting directory: $dir");
00179                 }
00180 
00181                 $this->LocaleID = $localeID;
00182 
00183                 // Load or create root project
00184                 $rootProject = new TranslationProject();
00185 
00186                 // Do we have a root for our project?
00187                 if(!$rootProject->LoadRoot("Xaraya", $this->LocaleID))
00188                 {
00189                         // No root yet, create it now.
00190                         $rootProject->CreateRootProject("Xaraya", $this->LocaleID);
00191                 }
00192                 
00193                 // Save new project or update old one's timestamp
00194                 if(!$rootProject->Save())
00195                 {
00196                         throw new Exception("Cannot create root project for Xaraya.");
00197                 }
00198 
00199                 // Start the recursive madness
00200                 $dirIterator = new RecursiveDirectoryIterator($dir);
00201                 $this->ProcessDir($dirIterator, $rootProject);
00202         }
00203 }
00204 
00205 ?>

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