/** * 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 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; }
/** * 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 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 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; }