XarayaTranslationMemory  1.6
TranslationImportXaraya.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************************
3  * Copyright (C) 2005-2020 by Ferenc Veres *
4  * lion@netngine.hu *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 3 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18  ***************************************************************************/
19 include_once("lib/TranslationProject.php");
20 
42 {
43  protected $ProjectName = "Xaraya";
44  protected $XmlNameSpace = "http://xaraya.com/2002/ns/translations";
45 
46  private $LocaleID;
47 
52  function __construct()
53  {
54  }
55 
63  private function ImportFile($fullName, TranslationProject $thisProject)
64  {
65  // Load the XML file
66  $langXml = new DOMDocument();
67  if(!$langXml->load($fullName))
68  {
69  throw new Exception("Can't load language XML file.\n");
70  }
71 
72  // Store the document in the project
73  $document = $langXml->saveXML();
74  $thisProject->SetDocument($document);
75 
76  // XPath to all translation entries
77  $xpath = new DOMXPath($langXml);
78  $xpath->registerNamespace("m", $this->XmlNameSpace);
79  $entryNodes = $xpath->query("//m:translations/m:entry");
80 
81  $projID = $thisProject->GetID();
82  $locID = $thisProject->GetLocaleID();
83 
84  $rowCount = 0;
85 
86  // Loop on each string
87  foreach ($entryNodes as $entry)
88  {
89  $origNodes = $xpath->query("m:string", $entry);
90  $transNodes = $xpath->query("m:translation", $entry);
91 
92  if($origNodes->item(0)->nodeValue == "")
93  {
94  echo "Empty Original node in the current file.\n";
95  }
96  else
97  {
98  // Stores translation and create index if not yet done
99  $str = new TranslationString(
100  0,
101  $projID,
102  $origNodes->item(0)->nodeValue,
103  $locID,
104  $transNodes->item(0)->nodeValue,
105  0);
106  $str->SetRowOrder($rowCount++);
107  $id = $str->Save();
108 
109  $origNodes->item(0)->nodeValue = "###${id}###";
110  $transNodes->item(0)->nodeValue = "###${id}t###";
111 
112  }
113  }
114 
115  // Store the finished skeleton in the project
116  $skeleton = $langXml->saveXML();
117  $thisProject->SetSkeleton($skeleton);
118  $thisProject->Save();
119  }
120 
131  function ProcessDir(RecursiveDirectoryIterator $dirIterator, TranslationProject $parentProject)
132  {
133  $root = ($parentProject->GetRootID() == 0 ? $parentProject->GetID() : $parentProject->GetRootID());
134 
135  foreach($dirIterator as $file)
136  {
137  $fname = preg_replace('/.*\//', '', $file->getFilename()); // FIXME: required due to a bug in php5.1.2
138  if($fname == "." || $fname == "..")
139  {
140  continue;
141  }
142 
143  //if($file->isDir() || ($file->isFile() && preg_match("/\.xml$/", $fname)))
144  //{
145  // Create new subproject for ourselves or get existing one
146  $newProject = new TranslationProject();
147 
148  if(!$newProject->Load($parentProject->GetID(), $fname, $parentProject->GetLocaleID(), false))
149  {
150  $newProject->CreateProject($root, $parentProject->GetID(), $fname, $file->isDir(), $parentProject->GetLocaleID());
151  }
152 
153  // Save new or update existing one's timestamp
154  $newProject->Save();
155 
156  // Process file
157  if($file->isFile())
158  {
159  if(preg_match("/\.xml$/", $fname))
160  {
161  // Process locale files.
162  echo "process_file(".$file->getPathname().")\n";
163  $this->ImportFile($file->getPathname(), $newProject);
164  }
165  else
166  {
167  // Store nonXML files (e.g. index.html)
168  echo "store file(".$file->getPathname().")\n";
169  $newProject->SetDocument(file_get_contents($file->getPathname()));
170  $newProject->Save();
171  }
172  }
173 
174  // Go for subdirs
175  if($file->isDir())
176  {
177  $this->ProcessDir($dirIterator->getChildren(), $newProject);
178  }
179  //}
180  }
181  }
182 
190  function Import($localeName)
191  {
192  $localeID = TranslationProject::$LocaleIDCache[$localeName];
193  if(empty($localeID))
194  {
195  throw new Exception("ExportXaraya called with unknown locale name: $localeName");
196  }
197 
198  // Clear cached project lists
199  ProjectIterator::ClearCache($localeID);
200 
201 
202  $dir = GetConfigVar('import_path')."/".$localeName."/xml/";
203  if(!is_dir($dir))
204  {
205  throw new Exception("ExportXaraya using nonexisting directory: $dir");
206  }
207 
208  $this->LocaleID = $localeID;
209 
210  // Load or create root project
211  $rootProject = new TranslationProject();
212 
213  // Do we have a root for our project?
214  if(!$rootProject->LoadRoot($this->ProjectName, $this->LocaleID))
215  {
216  // No root yet, create it now.
217  $rootProject->CreateRootProject($this->ProjectName, $this->LocaleID);
218  }
219 
220  // Save new project or update old one's timestamp
221  if(!$rootProject->Save())
222  {
223  throw new Exception("Cannot create root project for Xaraya.");
224  }
225 
226  // Start the recursive madness
227  $dirIterator = new RecursiveDirectoryIterator($dir);
228  $this->ProcessDir($dirIterator, $rootProject);
229  }
230 }
231 
232 ?>
ImportFile($fullName, TranslationProject $thisProject)
static ClearCache($localeID)
GetConfigVar($varName)
Definition: Common.php:84
ProcessDir(RecursiveDirectoryIterator $dirIterator, TranslationProject $parentProject)