public function testFromArray()
 {
     $data = array('de' => array('file' => array('filename' => 'test.de.txt', 'contents' => 'Testdaten'), 'key' => array('test_translation_unit' => 'Testübersetzung')), 'en' => array('file' => array('filename' => 'test.en.txt', 'contents' => 'Test Data'), 'key' => array('test_translation_unit' => 'Test translation')));
     $this->object->fromArray($data);
     if (!$this->object->store()) {
         $this->fail("storing failed");
     }
     $tmxFile = new Application_Util_TmxFile();
     $tmxFile->load(APPLICATION_PATH . DIRECTORY_SEPARATOR . $this->tmxTarget);
     $tmxArray = $tmxFile->toArray();
     $this->assertTrue(isset($tmxArray['test_translation_unit']), "Expected translation unit 'test_translation_unit'");
     $this->assertEquals('Testübersetzung', $tmxArray['test_translation_unit']['de']);
     $this->assertEquals('Test translation', $tmxArray['test_translation_unit']['en']);
     $this->assertEquals('Testdaten', file_get_contents(APPLICATION_PATH . DIRECTORY_SEPARATOR . $this->contentBasepath . DIRECTORY_SEPARATOR . 'test.de.txt'));
     $this->assertEquals('Test Data', file_get_contents(APPLICATION_PATH . DIRECTORY_SEPARATOR . $this->contentBasepath . DIRECTORY_SEPARATOR . 'test.en.txt'));
 }
Beispiel #2
0
 /**
  * Store all tmx and content that has been set.
  * This is performed as a transaction, reverting
  * changes already written if one write fails.
  *
  * @return bool Returns true on success, false on failure.
  */
 public function store()
 {
     $result = true;
     $tmxBackup = $savedContent = array();
     try {
         if (!empty($this->_tmxContents)) {
             $this->verifyWriteAccess($this->_tmxTarget);
             $tmxFile = new Application_Util_TmxFile();
             if (is_file($this->_tmxTarget)) {
                 $tmxFile->load($this->_tmxTarget);
                 // backup in case a write operation fails
                 $tmxBackup = $tmxFile->toArray();
             }
             $tmxFile->fromArray($this->_tmxContents);
             $result = $tmxFile->save($this->_tmxTarget);
             if (!$result) {
                 throw new Setup_Model_Exception("Saving File '{$this->_tmxTarget}' failed");
             }
         }
         foreach ($this->_contentSources as $filename => $contents) {
             if (!is_null($contents)) {
                 $this->verifyWriteAccess($filename);
                 // backup stored content to restore in case a write operation fails
                 $savedContent[$filename] = file_get_contents($filename);
                 $result = false !== file_put_contents($filename, $contents) && $result;
                 if (!$result) {
                     throw new Setup_Model_Exception("Saving File '{$filename}' failed");
                 }
             }
         }
     } catch (Setup_Model_Exception $se) {
         if (!empty($tmxBackup)) {
             $tmxFile = new Application_Util_TmxFile();
             $tmxFile->fromArray($tmxBackup);
             $tmxFile->save($this->_tmxTarget);
         }
         if (!empty($savedContent)) {
             foreach ($savedContent as $filename => $contents) {
                 file_put_contents($filename, $contents);
             }
         }
         $this->log($se->getMessage());
         $result = false;
     }
     return $result;
 }
 /**
  * @see Description in abstract base class
  */
 public function fromArray(array $data)
 {
     $resultData = array();
     $resultTmx = new Application_Util_TmxFile();
     foreach ($data as $language => $fields) {
         foreach ($fields as $key => $val) {
             switch ($key) {
                 case 'file':
                     if (!is_array($val) || !isset($val['filename']) || !isset($val['contents'])) {
                         throw new Setup_Model_Exception('Invalid data structure');
                     }
                     $filePath = $this->_contentBasepath . DIRECTORY_SEPARATOR . $val['filename'];
                     $resultData[$filePath] = $val['contents'];
                     break;
                 case 'key':
                     foreach ($val as $translationUnit => $variant) {
                         $resultTmx->setVariantSegment($translationUnit, $language, $variant);
                     }
                     break;
                 default:
                     throw new Setup_Model_Exception('Failed loading array. Invalid data structure.');
             }
         }
     }
     $this->setContent($resultData);
     $this->setTranslation($resultTmx->toArray());
 }
 /**
  * Returns translations in modules set via @see setModules()
  * and (optionally) matching string set via @see setFilter()
  *
  * @param string $sortKey   Key used to sort result array.
  *                          Valid keys are defined as class constants
  * @param int $sortOrder    Sort order as expected by @see array_multisort()
  * @throw Setup_Model_FileNotReadableException Thrown if loading tmx file(s) fails.
  */
 public function getTranslations($sortKey = self::SORT_UNIT, $sortOrder = SORT_ASC)
 {
     $fileData = $this->getFiles();
     $translations = array();
     $sortArray = array();
     foreach ($fileData as $module => $files) {
         foreach ($files as $dir => $filenames) {
             foreach ($filenames as $fileName) {
                 $relativeFilePath = "{$module}/{$dir}/{$fileName}";
                 $filePath = APPLICATION_PATH . "/modules/{$relativeFilePath}";
                 $tmxFile = new Application_Util_TmxFile();
                 if ($tmxFile->load($filePath)) {
                     $translationUnits = $tmxFile->toArray();
                     foreach ($translationUnits as $key => $values) {
                         if (empty($this->_filter) || strpos($key, $this->_filter) !== false) {
                             foreach ($values as $lang => $value) {
                                 $row = array('unit' => $key, 'module' => $module, 'directory' => $dir, 'filename' => $fileName, 'language' => $lang, 'variant' => $value);
                                 $translations[] = $row;
                                 $sortArray[] = $row[$sortKey];
                             }
                         }
                     }
                 } else {
                     throw new Setup_Model_FileNotReadableException($filePath);
                 }
             }
         }
     }
     array_multisort($sortArray, $sortOrder, SORT_STRING, $translations);
     return $translations;
 }
Beispiel #5
0
 /**
  * @see Description in abstract base class
  */
 public function fromArray(array $data)
 {
     $resultData = array();
     $resultTmx = new Application_Util_TmxFile();
     foreach ($data as $translationUnit => $variants) {
         foreach ($variants as $language => $contents) {
             if (is_array($contents) && isset($contents['filename']) && isset($contents['contents'])) {
                 $filePath = $this->_contentBasepath . DIRECTORY_SEPARATOR . $contents['filename'];
                 $resultData[$filePath] = $contents['contents'];
                 $contents = $contents['filename'];
             }
             $resultTmx->setVariantSegment($translationUnit, $language, $contents);
         }
     }
     $this->setContent($resultData);
     $this->setTranslation($resultTmx->toArray());
 }