public function ImportFile($strTemplateFile, $strTranslatedFile = null) { if ($strTranslatedFile != '' && !file_exists($strTranslatedFile)) { NarroLogger::LogWarn(sprintf('Copying unsupported file type: %s', $strTemplateFile)); NarroImportStatistics::$arrStatistics['Unsupported files that were copied from the source language']++; if (@copy($strTemplateFile, $strTranslatedFile) == false) { NarroLogger::LogError(sprintf('Failed to copy "%s" to "%s"', $strTemplateFile, $strTranslatedFile)); } } }
public function ExportFile($strTemplateFile, $strTranslatedFile) { $arrSourceKey = $this->FileAsArray($strTemplateFile); $intElapsedTime = time() - $intTime; if ($intElapsedTime > 0) { // NarroLogger::LogDebug(sprintf('Preprocessing %s took %d seconds.', $this->objFile->FileName, $intElapsedTime)); } // NarroLogger::LogDebug(sprintf('Found %d contexts in file %s.', count($arrSourceKey), $this->objFile->FileName)); if (is_array($arrSourceKey)) { $arrTranslationObjects = NarroContextInfo::QueryArray(QQ::AndCondition(QQ::Equal(QQN::NarroContextInfo()->Context->FileId, $this->objFile->FileId), QQ::Equal(QQN::NarroContextInfo()->LanguageId, $this->objTargetLanguage->LanguageId), QQ::Equal(QQN::NarroContextInfo()->Context->Active, 1))); foreach ($arrTranslationObjects as $objNarroContextInfo) { $strTranslation = $this->GetExportedSuggestion($objNarroContextInfo); if (isset($arrSourceKey[$objNarroContextInfo->Context->Context])) { $arrSourceKey[$objNarroContextInfo->Context->Context]->Value = $strTranslation; } else { if ($this->blnSkipUntranslated == false) { $arrSourceKey[$objNarroContextInfo->Context->Context]->Value = $objNarroContextInfo->Context->Text->TextValue; } else { unset($arrTranslation[$objNarroContextInfo->Context->Context]); } } } $hndTranslationFile = fopen($strTranslatedFile, 'w'); foreach ($arrSourceKey as $strContext => $objEntity) { /* @var $objEntity NarroFileEntity */ fwrite($hndTranslationFile, $objEntity->Key . "\n"); fwrite($hndTranslationFile, $objEntity->Comment . "\n"); fwrite($hndTranslationFile, $objEntity->Value . "\n\n"); } fclose($hndTranslationFile); NarroUtils::Chmod($strTranslatedFile, 0666); return true; } else { NarroLogger::LogWarn(sprintf('Found a empty template (%s), copying the original', $strTemplateFile)); copy($strTemplateFile, $strTranslatedFile); NarroUtils::Chmod($strTranslatedFile, 0666); return false; } }
protected function GetWorkingDirectory() { if (!file_exists($this->fileSource->File)) { throw new Exception('You have to upload a file'); } $this->strWorkingDirectory = sprintf('%s/upload-u_%d-l_%s-p_%d', __TMP_PATH__, QApplication::GetUserId(), $this->objLanguage->LanguageCode, $this->objProject->ProjectId); $this->CleanWorkingDirectory(); mkdir($this->strWorkingDirectory); chmod($this->strWorkingDirectory, 0777); switch (strtolower(pathinfo($this->fileSource->File, PATHINFO_EXTENSION))) { case 'zip': case 'xpi': NarroLogger::LogInfo(sprintf('Trying to uncompress %s', $this->fileSource->FileName)); $objZipFile = new ZipArchive(); $intErrCode = $objZipFile->open($this->fileSource->File); if ($intErrCode === TRUE) { $objZipFile->extractTo($this->strWorkingDirectory); $objZipFile->close(); NarroLogger::LogInfo(sprintf('Sucessfully uncompressed %s.', $this->fileSource->FileName)); } else { switch ($intErrCode) { case ZIPARCHIVE::ER_NOZIP: $strError = 'Not a zip archive'; break; default: $strError = 'Error code: ' . $intErrCode; } $this->fileSource->File = ''; throw new Exception(sprintf('Failed to uncompress %s: %s', $this->fileSource->File, $strError)); } break; case 'dtd': case 'properties': case 'ini': case 'inc': case 'po': case 'sdf': case 'svg': case 'dpo': case 'srt': case 'php': copy($this->fileSource->File, $this->strWorkingDirectory . '/' . $this->fileSource->FileName); chmod($this->strWorkingDirectory . '/' . $this->fileSource->FileName, 0666); NarroLogger::LogInfo(sprintf('Single file uploaded, copied %s to %s', $this->fileSource->FileName, $this->strWorkingDirectory)); break; default: throw new Exception(sprintf('Unsupported file type uploaded')); } if (file_exists($this->fileSource->File)) { unlink($this->fileSource->File); } $this->fileSource->btnDelete_Click(); $arrSearchResult = NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, $this->objLanguage->LanguageCode); if ($arrSearchResult == false) { $arrSearchResult = NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, $this->objLanguage->LanguageCode . '-' . $this->objLanguage->CountryCode); } if ($arrSearchResult == false) { $arrSearchResult = NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, $this->objLanguage->LanguageCode . '_' . $this->objLanguage->CountryCode); } NarroUtils::RecursiveChmod($this->strWorkingDirectory); if (is_array($arrSearchResult) && count($arrSearchResult) == 1) { NarroLogger::LogWarn(sprintf('Path changed from "%s" to "%s" because a directory named "%s" was found deeper in the given path.', $this->strWorkingDirectory, $arrSearchResult[0], $this->objLanguage->LanguageCode)); $this->strWorkingDirectory = $arrSearchResult[0]; } if ($this->chkCopyFilesToDefaultDirectory->Checked) { if (file_exists(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode)) { NarroUtils::RecursiveDelete(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode . '/*'); } else { mkdir(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode, 0777); } NarroUtils::RecursiveCopy($this->strWorkingDirectory, __IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode); NarroUtils::RecursiveChmod(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode); } return $this->strWorkingDirectory; }
protected function GetWorkingDirectory($strCheckoutCommand = null) { $this->strWorkingDirectory = sprintf('%s/upload-u_%d-l_%s-p_%d', __TMP_PATH__, QApplication::GetUserId(), $this->objLanguage->LanguageCode, $this->objProject->ProjectId); $this->CleanWorkingDirectory(); if (!$this instanceof NarroMercurialSourcePanel) { mkdir($this->strWorkingDirectory); } $strCommand = sprintf($strCheckoutCommand, escapeshellarg($this->txtRepository->Text), escapeshellarg($this->strWorkingDirectory)); NarroLogger::LogInfo(sprintf('Running "%s"', $strCommand)); $strProcLogFile = __TMP_PATH__ . '/' . $this->objProject->ProjectId . '-' . $this->objLanguage->LanguageCode . '-vcs.log'; if (file_exists($strProcLogFile) && is_writable($strProcLogFile)) { unlink($strProcLogFile); } chdir(__TMP_PATH__); $mixProcess = proc_open("{$strCommand}", array(1 => array("file", $strProcLogFile, 'a'), 2 => array("file", $strProcLogFile, 'a')), $foo); $status = proc_get_status($mixProcess); while ($status['running']) { $status = proc_get_status($mixProcess); } proc_close($mixProcess); if (!file_exists($this->strWorkingDirectory)) { throw new Exception(sprintf('The working directory "%s" does not exist, probably the checkout command failed', $this->strWorkingDirectory)); } chmod($this->strWorkingDirectory, 0777); if (file_exists($strProcLogFile)) { NarroLogger::LogInfo(file_get_contents($strProcLogFile)); } NarroUtils::RecursiveDelete($this->strWorkingDirectory . '/.hg'); foreach (NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, '.svn') as $strSvnDir) { NarroUtils::RecursiveDelete($strSvnDir); } $arrSearchResult = NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, $this->objLanguage->LanguageCode); if ($arrSearchResult == false) { $arrSearchResult = NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, $this->objLanguage->LanguageCode . '-' . $this->objLanguage->CountryCode); } if ($arrSearchResult == false) { $arrSearchResult = NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, $this->objLanguage->LanguageCode . '_' . $this->objLanguage->CountryCode); } NarroUtils::RecursiveChmod($this->strWorkingDirectory); if (is_array($arrSearchResult) && count($arrSearchResult) == 1) { NarroLogger::LogWarn(sprintf('Path changed from "%s" to "%s" because a directory named "%s" was found deeper in the given path.', $this->strWorkingDirectory, $arrSearchResult[0], $this->objLanguage->LanguageCode)); $this->strWorkingDirectory = $arrSearchResult[0]; } if ($this->chkCopyFilesToDefaultDirectory->Checked) { if (file_exists(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode)) { NarroUtils::RecursiveDelete(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode . '/*'); } else { mkdir(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode, 0777, true); } NarroUtils::RecursiveCopy($this->strWorkingDirectory, __IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode); NarroUtils::RecursiveChmod(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode); } return $this->strWorkingDirectory; }
public function Save($blnForceInsert = false, $blnForceUpdate = false) { $this->intSuggestionWordCount = NarroString::WordCount($this->strSuggestionValue); $this->intSuggestionCharCount = mb_strlen($this->strSuggestionValue); $this->strSuggestionValueMd5 = md5($this->strSuggestionValue); if (!isset($this->blnIsImported)) { $this->blnIsImported = false; } if (!$this->__blnRestored || $blnForceInsert) { $this->dttCreated = QDateTime::Now(); } else { $this->dttModified = QDateTime::Now(); } parent::Save($blnForceInsert, $blnForceUpdate); /** * Update all context infos with the has suggestion property */ $arrContextInfo = NarroContextInfo::QueryArray(QQ::AndCondition(QQ::Equal(QQN::NarroContextInfo()->Context->TextId, $this->intTextId), QQ::Equal(QQN::NarroContextInfo()->LanguageId, $this->intLanguageId), QQ::Equal(QQN::NarroContextInfo()->HasSuggestions, 0))); foreach ($arrContextInfo as $objOneContextInfo) { $objOneContextInfo->HasSuggestions = 1; $objOneContextInfo->Modified = QDateTime::Now(); try { $objOneContextInfo->Save(); } catch (Exception $objEx) { NarroLogger::LogWarn($objEx->getMessage()); } } }
public static function SetProgress($intValue, $intProjectId, $strOperation, $intFilesToProcess = 0, $intProgressPerFile = 0) { if ($intFilesToProcess == 0) { $intFilesToProcess = self::GetFilesToProcess($intProjectId, $strOperation); } if ($intProgressPerFile == 0) { $intProgressPerFile = self::GetProgressPerFile($intProjectId, $strOperation); } if ($intValue == 0) { $intValue = self::GetProgressPerProject($intProjectId, $strOperation); } if (!@file_put_contents(self::GetProgressFileName($intProjectId, $strOperation), $intFilesToProcess . ',' . $intValue . ',' . $intProgressPerFile)) { NarroLogger::LogWarn(sprintf('Can\'t write progress file %s', self::GetProgressFileName($intProjectId, $strOperation))); } @chmod(self::GetProgressFileName($intProjectId, $strOperation), 0666); }
public function ExportFile($strTemplateFile, $strTranslatedFile) { $intTime = time(); $arrSourceKey = $this->FileAsArray($strTemplateFile); $intElapsedTime = time() - $intTime; if ($intElapsedTime > 0) { // NarroLogger::LogDebug(sprintf('Preprocessing %s took %d seconds.', $this->objFile->FileName, $intElapsedTime)); } // NarroLogger::LogDebug(sprintf('Found %d contexts in file %s.', count($arrSourceKey), $this->objFile->FileName)); if (is_array($arrSourceKey)) { $arrSourceKey = $this->GetAccessKeys($arrSourceKey); $arrTranslation = $this->GetTranslations($arrSourceKey); $hndTranslationFile = fopen($strTranslatedFile, 'w'); if ($this->objProject->GetPreferenceValueByName('Export translators and reviewers in the file header as a comment') == 'Yes') { $arrUsers = array(); foreach ($this->objFile->GetTranslatorArray($this->objTargetLanguage->LanguageId) as $objUser) { $arrUsers[] = sprintf("# %s <%s>", $objUser->RealName, $objUser->Email); } if (count($arrUsers)) { fwrite($hndTranslationFile, sprintf("# Translator(s):\n#\n%s\n#\n", join("\n", $arrUsers))); } $arrUsers = array(); foreach ($this->objFile->GetReviewerArray($this->objTargetLanguage->LanguageId) as $objUser) { $arrUsers[] = sprintf("# %s <%s>", $objUser->RealName, $objUser->Email); } if (count($arrUsers)) { fwrite($hndTranslationFile, sprintf("# Reviewer(s):\n#\n%s\n#\n", join("\n", $arrUsers))); } } if ($this->objFile->Header) { fwrite($hndTranslationFile, $this->objFile->Header); } foreach ($arrSourceKey as $strContext => $objEntity) { if (isset($arrTranslation[$strContext])) { fwrite($hndTranslationFile, $objEntity->BeforeValue . $arrTranslation[$strContext] . $objEntity->AfterValue); } else { fwrite($hndTranslationFile, $objEntity->BeforeValue . $objEntity->Value . $objEntity->AfterValue); } } fclose($hndTranslationFile); NarroUtils::Chmod($strTranslatedFile, 0666); return true; } else { NarroLogger::LogWarn(sprintf('Found a empty template (%s), copying the original', $strTemplateFile)); copy($strTemplateFile, $strTranslatedFile); NarroUtils::Chmod($strTranslatedFile, 0666); return false; } }
/** * A translation here consists of the project, file, text, translation, context, plurals, approval, ignore equals * * @param string $strOriginal the original text * @param string $strOriginalAccKey access key for the original text * @param string $strTranslation the translated text from the import file (can be empty) * @param string $strOriginalAccKey access key for the translated text * @param string $strContext the context where the text/translation appears in the file * @param string $intPluralForm if this is a plural, what plural form is it (0 singular, 1 plural form 1, and so on) * @param string $strComment a comment from the imported file * * @return string valid suggestion */ protected function GetTranslation($strOriginal, $strOriginalAccKey = null, $strOriginalAccKeyPrefix = null, $strTranslation, $strTranslationAccKey = null, $strContext, $strComment = null) { $objNarroContextInfo = NarroContextInfo::QuerySingle(QQ::AndCondition(QQ::Equal(QQN::NarroContextInfo()->Context->ProjectId, $this->objProject->ProjectId), QQ::Equal(QQN::NarroContextInfo()->Context->FileId, $this->objFile->FileId), QQ::Equal(QQN::NarroContextInfo()->Context->ContextMd5, md5(trim($strContext))), QQ::Equal(QQN::NarroContextInfo()->Context->Text->TextValueMd5, md5($strOriginal)), QQ::Equal(QQN::NarroContextInfo()->LanguageId, $this->objTargetLanguage->LanguageId))); if ($objNarroContextInfo instanceof NarroContextInfo) { $strSuggestionValue = $this->GetExportedSuggestion($objNarroContextInfo); $arrResult = QApplication::$PluginHandler->ExportSuggestion($strOriginal, $strSuggestionValue, $strContext, $this->objFile, $this->objProject); if ($arrResult[1] != '' && $arrResult[0] == $strOriginal && $arrResult[2] == $strContext && $arrResult[3] == $this->objFile && $arrResult[4] == $this->objProject) { $strSuggestionValue = $arrResult[1]; } else { NarroLogger::LogWarn(sprintf('The plugin "%s" returned an unexpected result while processing the suggestion "%s": %s', QApplication::$PluginHandler->CurrentPluginName, $strTranslation, $strTranslation)); } if (!is_null($strOriginalAccKey) && !is_null($strOriginalAccKeyPrefix)) { /** * @todo don't export if there's no valid access key */ $strTextWithAccKey = NarroString::Replace($objNarroContextInfo->SuggestionAccessKey, $strOriginalAccKeyPrefix . $objNarroContextInfo->SuggestionAccessKey, $strSuggestionValue, 1); return $strTextWithAccKey; } else { return $strSuggestionValue; } } else { /** * leave it untranslated */ return $strOriginal; } }
/** * A translation here consists of the project, file, text, translation, context, plurals, approval, ignore equals * * @param string $strOriginal the original text * @param string $strOriginalAccKey access key for the original text * @param string $strTranslation the translated text from the import file (can be empty) * @param string $strOriginalAccKey access key for the translated text * @param string $strContext the context where the text/translation appears in the file * @param string $intPluralForm if this is a plural, what plural form is it (0 singular, 1 plural form 1, and so on) * @param string $strComment a comment from the imported file * * @return string valid suggestion */ protected function GetTranslation($strOriginal, $strOriginalAccKey = null, $strOriginalAccKeyPrefix = null, $strTranslation, $strTranslationAccKey = null, $strContext, $strComment = null) { /** * The contexts are trimmed at import to avoid useless white space contexts, so we need to trim it when searching for it as well */ $strContext = trim($strContext); if ($strContext != '') { $objNarroContextInfo = NarroContextInfo::QuerySingle(QQ::AndCondition(QQ::Equal(QQN::NarroContextInfo()->Context->ProjectId, $this->objProject->ProjectId), QQ::Equal(QQN::NarroContextInfo()->Context->FileId, $this->objFile->FileId), QQ::Equal(QQN::NarroContextInfo()->Context->Active, 1), QQ::Equal(QQN::NarroContextInfo()->Context->File->Active, 1), QQ::Equal(QQN::NarroContextInfo()->Context->TextAccessKey, $strOriginalAccKey), QQ::Equal(QQN::NarroContextInfo()->Context->ContextMd5, md5($strContext)), QQ::Equal(QQN::NarroContextInfo()->Context->Text->TextValueMd5, md5($strOriginal)), QQ::Equal(QQN::NarroContextInfo()->LanguageId, $this->objTargetLanguage->LanguageId))); } else { $objNarroContextInfo = NarroContextInfo::QuerySingle(QQ::AndCondition(QQ::Equal(QQN::NarroContextInfo()->Context->ProjectId, $this->objProject->ProjectId), QQ::Equal(QQN::NarroContextInfo()->Context->FileId, $this->objFile->FileId), QQ::Equal(QQN::NarroContextInfo()->Context->Active, 1), QQ::Equal(QQN::NarroContextInfo()->Context->File->Active, 1), QQ::Equal(QQN::NarroContextInfo()->Context->TextAccessKey, $strOriginalAccKey), QQ::Equal(QQN::NarroContextInfo()->Context->Text->TextValueMd5, md5($strOriginal)), QQ::Equal(QQN::NarroContextInfo()->LanguageId, $this->objTargetLanguage->LanguageId))); } if ($objNarroContextInfo instanceof NarroContextInfo) { $this->objCurrentContext = $objNarroContextInfo; $strSuggestionValue = $this->GetExportedSuggestion($objNarroContextInfo); if ($strSuggestionValue !== false) { $arrResult = QApplication::$PluginHandler->ExportSuggestion($strOriginal, $strSuggestionValue, $strContext, $this->objFile, $this->objProject); if ($arrResult[1] != '' && $arrResult[0] == $strOriginal && $arrResult[2] == $strContext && $arrResult[3] == $this->objFile && $arrResult[4] == $this->objProject) { $strSuggestionValue = $arrResult[1]; } else { NarroLogger::LogWarn(sprintf('The plugin "%s" returned an unexpected result while processing the suggestion "%s": %s', QApplication::$PluginHandler->CurrentPluginName, $strSuggestionValue, join(';', $arrResult))); } if (!is_null($strOriginalAccKey) && !is_null($strOriginalAccKeyPrefix)) { /** * @todo don't export if there's no valid access key */ $strTextWithAccKey = NarroString::Replace($objNarroContextInfo->SuggestionAccessKey, $strOriginalAccKeyPrefix . $objNarroContextInfo->SuggestionAccessKey, $strSuggestionValue, 1); return $strTextWithAccKey; } else { return $strSuggestionValue; } } else { // NarroLogger::LogDebug(sprintf('No translation found for the text "%s", while searching for context "%s"', $strOriginal, $strContext)); return ''; } } else { NarroLogger::LogError(sprintf('No context found for the text "%s", while searching for context "%s"', $strOriginal, $strContext)); return ''; } }
public function ExportFile($objFile, $strTemplateFile, $strTranslatedFile) { if (!$objFile instanceof NarroFile) { NarroLogger::LogWarn(sprintf('Failed to find a corresponding file in the database for %s', $strTemplateFile)); return false; } if (NarroFileProgress::CountByFileIdLanguageIdExport($objFile->FileId, $this->objTargetLanguage->LanguageId, 0)) { NarroLogger::LogWarn(sprintf('Not exporting %s based on the file settings.', $strTemplateFile)); return false; } $objFileImporter = NarroFileType::GetFileImporter($objFile->TypeId, $this); // NarroLogger::LogDebug( sprintf( t('Starting to export "%s"'), str_replace($this->objProject->DefaultTranslationPath, '', $strTranslatedFile) ) ); $objFileImporter->File = $objFile; QApplication::$PluginHandler->BeforeExportFile($objFile, $strTemplateFile, $strTranslatedFile); $blnMixResult = $objFileImporter->ExportFile($strTemplateFile, $strTranslatedFile); if (file_exists($strTranslatedFile)) { QApplication::$PluginHandler->AfterExportFile($objFile, $strTemplateFile, $strTranslatedFile); } $this->arrFileId[$objFile->FileId] = 1; return $blnMixResult; }
public function ImportFile($strTemplateFile, $strTranslatedFile = null) { $intTime = time(); if ($strTranslatedFile) { $arrTransKey = $this->FileAsArray($strTranslatedFile, false); } $arrSourceKey = $this->FileAsArray($strTemplateFile); $intElapsedTime = time() - $intTime; if ($intElapsedTime > 0) { // NarroLogger::LogDebug(sprintf('Preprocessing %s took %d seconds.', $this->objFile->FileName, $intElapsedTime)); } // NarroLogger::LogDebug(sprintf('Found %d contexts in file %s.', count($arrSourceKey), $this->objFile->FileName)); if (is_array($arrSourceKey)) { $arrSourceKey = $this->GetAccessKeys($arrSourceKey); if (isset($arrTransKey)) { $arrTransKey = $this->GetAccessKeys($arrTransKey); } foreach ($arrSourceKey as $strKey => $objEntity) { /* @var $objEntity NarroFileEntity */ // if it's a matched access key or command key, keep going if (isset($objEntity->LabelCtx)) { continue; } if (strstr($objEntity->Comment, 'DONT_TRANSLATE') !== false) { continue; } $this->AddTranslation($objEntity->Value, $objEntity->AccessKey, isset($arrTransKey[$strKey]) ? $arrTransKey[$strKey]->Value : null, isset($arrTransKey[$strKey]) ? isset($arrTransKey[$strKey]->AccessKey) ? $arrTransKey[$strKey]->AccessKey : null : null, trim($strKey), isset($objEntity->AccessKeyCtx) ? trim($objEntity->Comment) . "\n" . trim($arrSourceKey[$objEntity->AccessKeyCtx]->Comment) : trim($objEntity->Comment), $objEntity->CommandKey, isset($arrTransKey[$strKey]) ? isset($arrTransKey[$strKey]->CommandKey) ? $arrTransKey[$strKey]->CommandKey : null : null); } } else { NarroLogger::LogWarn(sprintf('Found a empty template (%s), copying the original', $strTemplateFile)); copy($strTemplateFile, $strTranslatedFile); chmod($strTranslatedFile, 0666); } }
public function ExportFile($strTemplateFile, $strTranslatedFile) { $hndTranslatedFile = @fopen($strTranslatedFile, 'w'); if (!$hndTranslatedFile) { throw new Exception(sprintf('Can\'t open file "%s" for writing', $strTranslatedFile)); } $intTotalToProcess = NarroUtils::CountFileLines($strTemplateFile); /** * get all the texts and contexts from the template file, including the file line */ $arrTexts = $this->FileToArray($strTemplateFile, $this->objSourceLanguage->LanguageCode, true); // NarroLogger::LogDebug(sprintf('Starting to process file "%s" (%d texts), the result is written to "%s".', $strTemplateFile, $intTotalToProcess, $strTranslatedFile)); $intFileLineNr = 0; foreach ($arrTexts as $strContext => $arrTextInfo) { $strText = $arrTextInfo[0]; $strTextAccKey = $arrTextInfo[1]; $strTextAccKeyPrefix = $arrTextInfo[2]; $strFileLine = $arrTextInfo[3]; $intFileLineNr++; $arrColumn = preg_split('/\\t/', $strFileLine); /** * Unset a number before language code */ $arrColumn[8] = ''; /** * create a copy for the translated line, we'll just replace lang code, the number on column 8 and the text with the translation */ $arrTranslatedColumn = $arrColumn; $arrTranslatedColumn[8] = 0; $arrTranslatedColumn[9] = 'ro'; $objNarroContextInfo = $this->GetContextInfo($strText, $strContext); if (!$objContextInfo) { // NarroLogger::LogDebug('No context info found, trying database'); $objContextInfo = NarroContextInfo::QueryArray(QQ::AndCondition(QQ::Equal(QQN::NarroContextInfo()->Context->FileId, $this->objFile->FileId), QQ::Equal(QQN::NarroContextInfo()->LanguageId, $this->objTargetLanguage->LanguageId), QQ::Equal(QQN::NarroContextInfo()->Context->Text->TextValueMd5, md5($strText)), QQ::Equal(QQN::NarroContextInfo()->Context->ContextMd5, md5($strContext)))); } /** * the original texts are used if no suggestion is found, so we export only approved texts */ if ($objNarroContextInfo instanceof NarroContextInfo) { $strSuggestionValue = $this->GetExportedSuggestion($objNarroContextInfo); } else { // NarroLogger::LogDebug('No context info found, skipping'); continue; } if (!isset($strSuggestionValue) || !$strSuggestionValue) { continue; } if ($objNarroContextInfo->Context->TextAccessKey != '') { if ($objNarroContextInfo->ValidSuggestionId && $objNarroContextInfo->SuggestionAccessKey != '') { $strSuggestionValue = NarroString::Replace($objNarroContextInfo->SuggestionAccessKey, $strTextAccKeyPrefix . $objNarroContextInfo->SuggestionAccessKey, $strSuggestionValue, 1); } else { $strSuggestionValue = $strTextAccKeyPrefix . $strSuggestionValue; } } $arrTranslatedColumn[10] = str_replace(array("\n", "\r"), array("", ""), $strSuggestionValue); preg_match_all('/\\\\"/', $strText, $arrEscOrigMatches); preg_match_all('/\\\\"/', $strSuggestionValue, $arrEscTransMatches); if (isset($arrEscOrigMatches[0]) && count($arrEscTransMatches[0]) % 2 != 0) { NarroLogger::LogWarn(sprintf('Warning! The translated text "%s" has unclosed double quotes.', $strSuggestionValue)); continue; } fwrite($hndTranslatedFile, join("\t", $arrColumn)); fwrite($hndTranslatedFile, join("\t", $arrTranslatedColumn)); } fclose($hndTranslatedFile); if (filesize($strTranslatedFile) == 0) { unlink($strTranslatedFile); } else { chmod($strTranslatedFile, 0666); } }
public function ExportFile($strTemplateFile, $strTranslatedFile) { $strTemplateContents = file_get_contents($strTemplateFile); if (!$strTemplateContents) { NarroLogger::LogWarn(sprintf('Found a empty template (%s), copying the original', $strTemplateFile)); copy($strTemplateFile, $strTranslatedFile); chmod($strTranslatedFile, 0666); return false; } if (strstr($strTemplateContents, '#define MOZ_LANGPACK_CONTRIBUTORS')) { $strTemplateContents = preg_replace('/^#\\s+#define MOZ_LANGPACK_CONTRIBUTORS.*$/m', '#define MOZ_LANGPACK_CONTRIBUTORS <em:contributor>Joe Solon</em:contributor> <em:contributor>Suzy Solon</em:contributor>', $strTemplateContents); } $arrTemplateContents = explode("\n", $strTemplateContents); $strComment = ''; foreach ($arrTemplateContents as $intPos => $strLine) { if (preg_match('/^#define\\s+([^\\s]+)\\s+(.+)$/s', trim($strLine), $arrMatches)) { $arrTemplate[trim($arrMatches[1])] = trim($arrMatches[2]); $arrTemplateLines[trim($arrMatches[1])] = $arrMatches[0]; $arrTemplateComment[trim($arrMatches[1])] = $strComment; $strComment = ''; } elseif (trim($strLine) != '' && $strLine[0] != '#') { // NarroLogger::LogDebug(sprintf('Skipped line "%s" from the template "%s".', $strLine, $this->objFile->FileName)); } elseif ($strLine[0] == '#') { $strComment .= "\n" . $strLine; } } $strTranslateContents = ''; if (count($arrTemplate) < 1) { return false; } $arrTranslationObjects = NarroContextInfo::QueryArray(QQ::AndCondition(QQ::Equal(QQN::NarroContextInfo()->Context->FileId, $this->objFile->FileId), QQ::Equal(QQN::NarroContextInfo()->LanguageId, $this->objTargetLanguage->LanguageId), QQ::Equal(QQN::NarroContextInfo()->Context->Active, 1))); foreach ($arrTranslationObjects as $objNarroContextInfo) { if ($objNarroContextInfo->ValidSuggestionId > 0) { $arrTranslation[$objNarroContextInfo->Context->Context] = $this->GetExportedSuggestion($objNarroContextInfo); if ($arrTranslation[$objNarroContextInfo->Context->Context] === false) { $arrTranslation[$objNarroContextInfo->Context->Context] = $objNarroContextInfo->Context->Text->TextValue; } if ($objNarroContextInfo->Context->TextAccessKey) { if ($objNarroContextInfo->SuggestionAccessKey) { $strAccessKey = $objNarroContextInfo->SuggestionAccessKey; } else { $strAccessKey = $objNarroContextInfo->Context->TextAccessKey; } $arrTranslation[$objNarroContextInfo->Context->Context] = preg_replace('/' . $strAccessKey . '/', '&' . $strAccessKey, $arrTranslation[$objNarroContextInfo->Context->Context], 1); NarroImportStatistics::$arrStatistics['Texts that have access keys']++; } else { NarroImportStatistics::$arrStatistics["Texts that don't have access keys"]++; } } else { // NarroLogger::LogDebug(sprintf('In file "%s", the context "%s" does not have a valid suggestion.', $this->objFile->FileName, $objNarroContextInfo->Context->Context)); NarroImportStatistics::$arrStatistics['Texts without valid suggestions']++; NarroImportStatistics::$arrStatistics['Texts kept as original']++; } } $strTranslateContents = $strTemplateContents; foreach ($arrTemplate as $strKey => $strOriginalText) { if (isset($arrTranslation[$strKey])) { $arrResult = QApplication::$PluginHandler->ExportSuggestion($strOriginalText, $arrTranslation[$strKey], $strKey, $this->objFile, $this->objProject); if ($arrResult[1] != '' && $arrResult[0] == $strOriginalText && $arrResult[2] == $strKey && $arrResult[3] == $this->objFile && $arrResult[4] == $this->objProject) { $arrTranslation[$strKey] = $arrResult[1]; } else { NarroLogger::LogWarn(sprintf('The plugin "%s" returned an unexpected result while processing the suggestion "%s": %s', QApplication::$PluginHandler->CurrentPluginName, $arrTranslation[$strKey], var_export($arrResult, true))); } if (strstr($strTranslateContents, sprintf('#define %s %s', $strKey, $strOriginalText))) { $strTranslateContents = str_replace(sprintf('#define %s %s', $strKey, $strOriginalText), sprintf('#define %s %s', $strKey, $arrTranslation[$strKey]), $strTranslateContents); } else { NarroLogger::LogWarn(sprintf('Can\'t find "%s" in the file "%s"'), $strKey . $strGlue . $strOriginalText, $this->objFile->FileName); } if (strstr($arrTranslation[$strKey], "\n")) { NarroLogger::LogWarn(sprintf('Skpping translation "%s" because it has a newline in it'), $arrTranslation[$strKey]); continue; } } else { // NarroLogger::LogDebug(sprintf('Couldn\'t find the key "%s" in the translations, using the original text.', $strKey, $this->objFile->FileName)); NarroImportStatistics::$arrStatistics['Texts kept as original']++; if ($this->blnSkipUntranslated == true) { } if ($this->blnSkipUntranslated == true) { if (isset($arrTemplateComment[$strKey]) && $arrTemplateComment[$strKey] != '') { $strTranslateContents = str_replace($arrTemplateComment[$strKey] . "\n", "\n", $strTranslateContents); $strTranslateContents = str_replace(sprintf("#define %s %s\n", $strKey, $strOriginalText), '', $strTranslateContents); } } } } if (file_exists($strTranslatedFile) && !is_writable($strTranslatedFile) && !unlink($strTranslatedFile)) { NarroLogger::LogError(sprintf('Can\'t delete the file "%s"', $strTranslatedFile)); } if (!file_put_contents($strTranslatedFile, $strTranslateContents)) { NarroLogger::LogError(sprintf('Can\'t write to file "%s"', $strTranslatedFile)); } @chmod($strTranslatedFile, 0666); }
public function btnKillProcess_Click($strFormId, $strControlId, $strParameter) { $strProcLogFile = __TMP_PATH__ . '/' . $this->objProject->ProjectId . '-' . QApplication::$TargetLanguage->LanguageCode . '-import-process.log'; $strProcPidFile = __TMP_PATH__ . '/' . $this->objProject->ProjectId . '-' . QApplication::$TargetLanguage->LanguageCode . '-import-process.pid'; if (!file_exists($strProcPidFile)) { NarroLogger::LogError('Could not find a pid file for the background process.'); $this->pnlLogViewer->MarkAsModified(); return false; } $intPid = file_get_contents($strProcPidFile); if (is_numeric(trim($intPid))) { $mixProcess = proc_open(sprintf('kill -9 %d', $intPid), array(2 => array("file", $strProcLogFile, 'a')), $foo); if ($mixProcess) { proc_close($mixProcess); NarroLogger::LogError('Process killed'); } else { NarroLogger::LogError('Failed to kill process'); } if (file_exists($strProcLogFile) && filesize($strProcLogFile)) { NarroLogger::LogWarn(sprintf('There are messages from the background process: %s', file_get_contents($strProcLogFile))); } $this->pnlLogViewer->MarkAsModified(); } }