/** * A translation here consists of the project, file, text, translation, context, 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 $strComment a comment from the imported file * @param string $strOriginalCmdKey command key for the original text * @param string $strTranslationCmdKey command key for the translated text */ protected function AddTranslation($strOriginal, $strOriginalAccKey = null, $strTranslation, $strTranslationAccKey = null, $strContext = '', $strComment = null, $strOriginalCmdKey = null, $strTranslationCmdKey = null) { if ($strOriginal == '') { return false; } $blnContextInfoChanged = false; $blnContextChanged = false; $objText = $this->GetText($strOriginal); if (!$objText instanceof NarroText) { $objText = NarroText::LoadByTextValueMd5(md5($strOriginal)); } if (!$this->blnOnlySuggestions && !$objText instanceof NarroText) { $objText = new NarroText(); $objText->TextValue = $strOriginal; try { $objText->Save(); // NarroLogger::LogDebug(sprintf('Added text "%s" from the file "%s"', $strOriginal, $this->objFile->FileName)); NarroImportStatistics::$arrStatistics['Imported texts']++; } catch (Exception $objExc) { NarroLogger::LogError(sprintf('Error while adding "%s": %s', $strOriginal, $objExc->getMessage())); NarroImportStatistics::$arrStatistics['Skipped contexts']++; NarroImportStatistics::$arrStatistics['Skipped suggestions']++; NarroImportStatistics::$arrStatistics['Skipped texts']++; NarroImportStatistics::$arrStatistics['Texts that had errors while adding']++; /** * If there's no text, there's no context and no suggestion */ return false; } } elseif (!$objText instanceof NarroText) { // NarroLogger::LogDebug('No text found'); /** * If there's no text, there's no context and no suggestion */ return false; } $objContext = $this->GetContext($strOriginal, $strContext, $strComment); if (!$objContext) { // NarroLogger::LogDebug('No context found, trying database'); $objContext = NarroContext::LoadByTextIdContextMd5FileIdCommentMd5($objText->TextId, md5($strContext), $this->objFile->FileId, md5($strComment)); } if (!$this->blnOnlySuggestions && !$objContext instanceof NarroContext) { // NarroLogger::LogDebug('Still no context, creating one'); $objContext = new NarroContext(); $objContext->TextId = $objText->TextId; $objContext->ProjectId = $this->objProject->ProjectId; $objContext->Context = $strContext; $objContext->ContextMd5 = md5($strContext); $objContext->FileId = $this->objFile->FileId; $objContext->Active = 1; $objContext->Modified = QDateTime::Now(); $objContext->Created = QDateTime::Now(); $objContext->Comment = $strComment; $objContext->CommentMd5 = md5($strComment); try { $objContext->Save(); } catch (Exception $objException) { NarroLogger::LogError(sprintf('An error occurred while saving the context: %s. Skipping the text "%s"', $objException->getMessage(), $strOriginal)); return false; } // NarroLogger::LogDebug(sprintf('Added the context "%s" from the file "%s"', nl2br($strContext), $this->objFile->FileName)); NarroImportStatistics::$arrStatistics['Imported contexts']++; $this->arrContextId[$objContext->ContextId] = $objContext->ContextId; } elseif ($objContext instanceof NarroContext) { // NarroLogger::LogDebug('Found context'); $this->arrContextId[$objContext->ContextId] = $objContext->ContextId; NarroImportStatistics::$arrStatistics['Reused contexts']++; } else { // NarroLogger::LogDebug(sprintf('Cannot add the context for text "%s" from the file "%s" because the importer is running with the option to import only suggestions.', $objText->TextValue, $this->objFile->FilePath)); return false; } /** * load the context info */ $objContextInfo = $this->GetContextInfo($strOriginal, $strContext, $strComment); if (!$objContextInfo) { // NarroLogger::LogDebug('No context info found, trying database'); $objContextInfo = NarroContextInfo::LoadByContextIdLanguageId($objContext->ContextId, $this->objTargetLanguage->LanguageId); } /** * Add context infos even if only suggestion is selected to allow users that have permissions only on one language to approve suggestions */ if (!$objContextInfo instanceof NarroContextInfo) { // NarroLogger::LogDebug('Still no context info, creating one'); $objContextInfo = new NarroContextInfo(); $objContextInfo->ContextId = $objContext->ContextId; $objContextInfo->LanguageId = $this->objTargetLanguage->LanguageId; $objContextInfo->HasSuggestions = 0; $objContextInfo->Created = QDateTime::Now(); $blnContextInfoChanged = true; } elseif ($objContextInfo instanceof NarroContextInfo) { // NarroLogger::LogDebug('Found context info'); NarroImportStatistics::$arrStatistics['Reused context informations']++; } if ($objContext instanceof NarroContext && $objContextInfo instanceof NarroContextInfo) { /** * this lies outside the if/else if reusing contexts is activated, so if a context was moved in another file, we'll just update the file_id */ if ($objContext->FileId != $this->objFile->FileId) { // NarroLogger::LogDebug('Context changed file'); $blnContextChanged = true; $objContext->FileId = $this->objFile->FileId; } if ($objContext->Active == false) { // NarroLogger::LogDebug('Context is inactive'); $blnContextChanged = true; $objContext->Active = true; } if ($objContext->TextAccessKey != $strOriginalAccKey) { // NarroLogger::LogDebug('Text access key changed for this context'); $blnContextChanged = true; $objContext->TextAccessKey = $strOriginalAccKey; } if ($objContext->TextCommandKey != $strOriginalCmdKey) { // NarroLogger::LogDebug('Text command key changed for this context'); $blnContextChanged = true; $objContext->TextCommandKey = $strOriginalCmdKey; } } if ($strTranslation == '') { // NarroLogger::LogDebug('No translation'); /** * just ignore, used for import without suggestions */ NarroImportStatistics::$arrStatistics['Texts without suggestions']++; } elseif ($this->blnCheckEqual && $strOriginal == $strTranslation) { // NarroLogger::LogDebug(sprintf('Skipped "%s" because "%s" has the same value. From "%s".', $strOriginal, $strTranslation, $this->objFile->FileName)); NarroImportStatistics::$arrStatistics['Skipped suggestions']++; NarroImportStatistics::$arrStatistics['Suggestions that kept the original text']++; } elseif ($this->blnImportSuggestions) { /** * See if a suggestion already exists, fetch it */ $objSuggestion = $this->GetSuggestion($strOriginal, $strTranslation); if (!$objSuggestion) { // NarroLogger::LogDebug('No translation, querying the database'); $objSuggestion = NarroSuggestion::LoadByTextIdLanguageIdSuggestionValueMd5($objText->TextId, $this->objTargetLanguage->LanguageId, md5($strTranslation)); } if (!$objSuggestion instanceof NarroSuggestion) { // NarroLogger::LogDebug('Still no translation, creating one'); $objSuggestion = new NarroSuggestion(); $objSuggestion->IsImported = 1; $objSuggestion->UserId = $this->objUser->UserId; $objSuggestion->TextId = $objText->TextId; $objSuggestion->LanguageId = $this->objTargetLanguage->LanguageId; $objSuggestion->SuggestionValue = $strTranslation; try { $objSuggestion->Save(); } catch (Exception $objException) { NarroLogger::LogError(sprintf('An error occurred while adding the suggestion "%s": %s. Skipping the text "%s"', $strTranslation, $objException->getMessage(), $strOriginal)); return false; } /** * update the HasSuggestions if it was 0 and we added a suggestion */ if ($objContextInfo instanceof NarroContextInfo && $objContextInfo->HasSuggestions == 0 && $objSuggestion instanceof NarroSuggestion) { $objContextInfo->HasSuggestions = 1; $blnContextInfoChanged = true; } NarroImportStatistics::$arrStatistics['Imported suggestions']++; } else { // NarroLogger::LogDebug('Found translation in the database'); NarroImportStatistics::$arrStatistics['Reused suggestions']++; } if ($objContextInfo instanceof NarroContextInfo && $this->blnApprove && (is_null($objContextInfo->ValidSuggestionId) || $this->blnApproveAlreadyApproved) && $objContextInfo->ValidSuggestionId != $objSuggestion->SuggestionId) { // NarroLogger::LogDebug('Approving translation'); $objContextInfo->ValidSuggestionId = $objSuggestion->SuggestionId; $objContextInfo->ValidatorUserId = QApplication::GetUserId(); $blnContextInfoChanged = true; NarroImportStatistics::$arrStatistics['Approved suggestions']++; } if ($objContextInfo instanceof NarroContextInfo && !is_null($strTranslationAccKey) && $objContextInfo->SuggestionAccessKey != $strTranslationAccKey) { // NarroLogger::LogDebug('Translation access key changed'); $blnContextInfoChanged = true; $objContextInfo->SuggestionAccessKey = $strTranslationAccKey; } if ($objContextInfo instanceof NarroContextInfo && !is_null($strTranslationCmdKey) && $objContextInfo->SuggestionCommandKey != $strTranslationCmdKey) { // NarroLogger::LogDebug('Translation command key changed'); $blnContextInfoChanged = true; $objContextInfo->SuggestionCommandKey = $strTranslationCmdKey; } } if ($blnContextInfoChanged && $objContextInfo instanceof NarroContextInfo) { // NarroLogger::LogDebug('Context info modified, saving...'); $objContextInfo->Modified = QDateTime::Now(); try { $objContextInfo->Save(); } catch (Exception $objExc) { NarroLogger::LogError(sprintf('Error while saving context info for context %s: %s', $strContext, $objExc->getMessage())); NarroImportStatistics::$arrStatistics['Skipped context infos']++; } } if ($blnContextChanged && $objContext instanceof NarroContext) { // NarroLogger::LogDebug('Context modified, saving...'); $objContext->Modified = QDateTime::Now(); try { $objContext->Save(); } catch (Exception $objExc) { NarroLogger::LogError(sprintf('Error while saving context %s: %s', $strContext, $objExc->getMessage())); NarroImportStatistics::$arrStatistics['Skipped contexts']++; } } return true; }