コード例 #1
0
        protected override void Execute()
        {
            var projects = _controllers.TranscreateController.GetSelectedProjects();

            if (projects?.Count != 1)
            {
                Enabled = false;
                return;
            }

            var project = projects[0];

            if (project is BackTranslationProject)
            {
                return;
            }

            var studioProject = _controllers.ProjectsController.GetProjects()
                                .FirstOrDefault(a => a.GetProjectInfo().Id.ToString() == project.Id);

            if (studioProject == null)
            {
                return;
            }

            var studioProjectInfo = studioProject.GetProjectInfo();


            var backTranslationsFolder = Path.Combine(studioProjectInfo.LocalProjectFolder, "BackProjects");

            if (Directory.Exists(backTranslationsFolder))
            {
                var message01 = "The Back-Translations folder is not empty."
                                + Environment.NewLine + Environment.NewLine
                                + "'" + backTranslationsFolder + "'"
                                + Environment.NewLine + Environment.NewLine;
                var message02 = "Do you want to proceed and delete this folder?";

                var response = MessageBox.Show(message01 + message02, PluginResources.Plugin_Name,
                                               MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (response != DialogResult.Yes)
                {
                    return;
                }

                TryDeleteDirectory(backTranslationsFolder);
            }


            var taskContexts = new List <TaskContext>();

            var progressSettings = new ProgressDialogSettings(ApplicationInstance.GetActiveForm(), true, true, true);
            var result           = ProgressDialog.Execute("Create Back-Translation Projects", () =>
            {
                ProgressDialog.Current.Report(0, "Reading language files...");

                var dateTimeStamp         = DateTime.UtcNow;
                var dataTimeStampToString = DateTimeStampToString(dateTimeStamp);
                var workFlowPath          = GetPath(studioProjectInfo.LocalProjectFolder, "WorkFlow");
                var workingActionPath     = GetPath(workFlowPath, "Convert");
                var workingFolder         = GetPath(workingActionPath, dataTimeStampToString);

                var exportOptions = new ExportOptions();
                exportOptions.IncludeBackTranslations = true;
                exportOptions.IncludeTranslations     = true;
                exportOptions.CopySourceToTarget      = false;

                var importOptions = new ImportOptions();
                importOptions.OverwriteTranslations      = true;
                importOptions.OriginSystem               = "Transcreate Automation";
                importOptions.StatusTranslationUpdatedId = string.Empty;

                var analysisBands = _projectAutomationService.GetAnalysisBands(studioProject);

                var sdlxliffReader = new SdlxliffReader(_segmentBuilder, exportOptions, analysisBands);
                var sdlxliffWriter = new SdlxliffWriter(_segmentBuilder, importOptions, analysisBands);
                var xliffWriter    = new XliffWriter(Enumerators.XLIFFSupport.xliff12sdl);

                // Read the SDLXLIFF data
                var fileDataList = GetFileDataList(project, studioProjectInfo, sdlxliffReader);
                var filesWithEmptyTranslations = fileDataList.Count(a => a.HasEmptyTranslations);
                if (filesWithEmptyTranslations > 0)
                {
                    var message01 = string.Format(PluginResources.Found_empty_translations_in_0_files,
                                                  filesWithEmptyTranslations)
                                    + Environment.NewLine + Environment.NewLine;
                    var message02 = PluginResources.Proceed_and_copy_source_to_target_for_empty_translations;

                    var response = MessageBox.Show(message01 + message02, PluginResources.Plugin_Name,
                                                   MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (response != DialogResult.Yes)
                    {
                        return;
                    }
                }

                ProgressDialog.Current.ProgressBarIsIndeterminate = false;
                decimal maximum = project.TargetLanguages.Count;
                decimal current = 0;

                foreach (var targetLanguage in project.TargetLanguages)
                {
                    if (ProgressDialog.Current.CheckCancellationPending())
                    {
                        ProgressDialog.Current.ThrowIfCancellationPending();
                    }

                    current++;
                    var progress = current / maximum * 100;
                    ProgressDialog.Current.Report((int)progress, "Language: " + targetLanguage.CultureInfo.DisplayName);


                    var sourceFiles    = new List <string>();
                    var languageFolder = GetPath(workingFolder, targetLanguage.CultureInfo.Name);

                    var targetFiles = project.ProjectFiles.Where(a =>
                                                                 string.Compare(a.TargetLanguage, targetLanguage.CultureInfo.Name,
                                                                                StringComparison.CurrentCultureIgnoreCase) == 0);

                    var languageFileData = new List <FileData>();
                    foreach (var projectFile in targetFiles)
                    {
                        var fileData =
                            fileDataList.FirstOrDefault(a => a.Data.DocInfo.DocumentId == projectFile.FileId);
                        if (fileData == null)
                        {
                            continue;
                        }

                        SwitchSourceWithTargetSegments(fileData);

                        var xliffFolder   = GetPath(languageFolder, projectFile.Path);
                        var xliffFilePath = Path.Combine(xliffFolder,
                                                         projectFile.Name.Substring(0, projectFile.Name.Length - ".sdlxliff".Length));

                        // Write the XLIFF file
                        var success = xliffWriter.WriteFile(fileData.Data, xliffFilePath, true);
                        if (!success)
                        {
                            throw new Exception(string.Format(
                                                    PluginResources.Unexpected_error_while_converting_the_file, xliffFilePath));
                        }

                        sourceFiles.Add(xliffFilePath);
                        languageFileData.Add(fileData);
                    }

                    var iconPath = GetBackTranslationIconPath();

                    var newStudioProject = _projectAutomationService.CreateBackTranslationProject(
                        studioProject, targetLanguage.CultureInfo.Name, iconPath, sourceFiles, "BT");

                    _projectAutomationService.RunPretranslationWithoutTm(newStudioProject);

                    var taskContext = CreateBackTranslationTaskContext(newStudioProject, languageFileData,
                                                                       studioProjectInfo.LocalProjectFolder, sdlxliffReader, sdlxliffWriter, xliffWriter);

                    taskContext.Completed = true;
                    taskContexts.Add(taskContext);
                }
            }, progressSettings);

            if (result.Cancelled || result.OperationFailed)
            {
                TryDeleteDirectory(backTranslationsFolder);

                var message = result.Cancelled ? "Process cancelled by user." : result.Error?.Message;
                MessageBox.Show(message, PluginResources.Plugin_Name);
                return;
            }

            foreach (var taskContext in taskContexts)
            {
                CleanupProjectSettings(taskContext.FileBasedProject);

                ActivateProject(taskContext.FileBasedProject);
                _projectAutomationService.RemoveLastReportOfType("Translate");

                var reports = _controllers.TranscreateController.CreateHtmlReports(taskContext, taskContext.FileBasedProject, taskContext.Project);
                _controllers.TranscreateController.ReportsController.AddReports(_controllers.TranscreateController.ClientId, reports);

                _controllers.TranscreateController.UpdateBackTranslationProjectData(project, taskContext);
            }

            _controllers.TranscreateController.InvalidateProjectsContainer();

            Enabled = false;
        }
コード例 #2
0
        protected override void ConfigureConverter(ProjectFile projectFile, IMultiFileConverter multiFileConverter)
        {
            var languageName = projectFile.Language.CultureInfo.Name;

            var targetFile = _wizardContext.ProjectFiles.FirstOrDefault(a =>
                                                                        a.FileId == projectFile.Id.ToString() && a.TargetLanguage == languageName);

            if (targetFile != null)
            {
                if (!targetFile.Selected)
                {
                    return;
                }

                targetFile.Location = Path.Combine(targetFile.Project.Path, targetFile.Location.Trim('\\'));

                if (string.IsNullOrEmpty(_currentLanguage) || languageName != _currentLanguage)
                {
                    _logReport.AppendLine();
                    _logReport.AppendLine(string.Format(PluginResources.Label_Language, languageName));
                    _currentLanguage = languageName;
                }

                var sdlxliffReader = new SdlxliffReader(_segmentBuilder,
                                                        _exportSettings.ExportOptions,
                                                        GetAnalysisBands(Project as FileBasedProject));

                var xliffWriter = new XliffWriter(_exportSettings.ExportOptions.XliffSupport);

                var dateTimeStampToString = GetDateTimeStampToString(_exportSettings.DateTimeStamp);
                var workingFolder         = Path.Combine(_exportSettings.TransactionFolder, dateTimeStampToString);
                var languageFolder        = Path.Combine(workingFolder, languageName);

                var xliffFolder   = GetXliffFolder(languageFolder, targetFile);
                var xliffFilePath = Path.Combine(xliffFolder, targetFile.Name + ".xliff");

                _logReport.AppendLine(string.Format(PluginResources.label_SdlXliffFile, targetFile.Location));
                _logReport.AppendLine(string.Format(PluginResources.label_XliffFile, xliffFilePath));

                try
                {
                    var xliffData = sdlxliffReader.ReadFile(_projectInfo.Id.ToString(), targetFile.Location);
                    var exported  = xliffWriter.WriteFile(xliffData, xliffFilePath, _exportSettings.ExportOptions.IncludeTranslations);

                    if (exported)
                    {
                        targetFile.Date                        = new DateTime(_exportSettings.DateTimeStamp.Ticks, DateTimeKind.Utc);
                        targetFile.Action                      = Enumerators.Action.Export;
                        targetFile.Status                      = Enumerators.Status.Success;
                        targetFile.XliffFilePath               = xliffFilePath;
                        targetFile.ConfirmationStatistics      = sdlxliffReader.ConfirmationStatistics;
                        targetFile.TranslationOriginStatistics = sdlxliffReader.TranslationOriginStatistics;
                    }

                    var activityFile = new ProjectFileActivity
                    {
                        ProjectFileId               = targetFile.FileId,
                        ProjectFile                 = targetFile,
                        ActivityId                  = Guid.NewGuid().ToString(),
                        Action                      = Enumerators.Action.Export,
                        Status                      = exported ? Enumerators.Status.Success : Enumerators.Status.Error,
                        Date                        = targetFile.Date,
                        Name                        = Path.GetFileName(targetFile.XliffFilePath),
                        Path                        = Path.GetDirectoryName(targetFile.XliffFilePath),
                        ConfirmationStatistics      = targetFile.ConfirmationStatistics,
                        TranslationOriginStatistics = targetFile.TranslationOriginStatistics
                    };

                    targetFile.ProjectFileActivities.Add(activityFile);

                    if (!exported)
                    {
                        _isError = true;
                    }

                    _logReport.AppendLine(string.Format(PluginResources.Label_Success, exported));
                    _logReport.AppendLine();
                }
                catch (Exception ex)
                {
                    _logReport.AppendLine();
                    _logReport.AppendLine(string.Format(PluginResources.label_ExceptionMessage, ex.Message));

                    throw;
                }
            }
        }
コード例 #3
0
        private TaskContext CreateBackTranslationTaskContext(FileBasedProject newStudioProject,
                                                             IReadOnlyCollection <FileData> languageFileData, string localProjectFolder,
                                                             SdlxliffReader sdlxliffReader, SdlxliffWriter sdlxliffWriter, XliffWriter xliffWriter)
        {
            var newStudioProjectInfo = newStudioProject.GetProjectInfo();

            var action    = Enumerators.Action.CreateBackTranslation;
            var workFlow  = Enumerators.WorkFlow.Internal;
            var setttings = GetSettings();

            var taskContext = new TaskContext(action, workFlow, setttings);

            taskContext.AnalysisBands = _projectAutomationService.GetAnalysisBands(newStudioProject);
            taskContext.ExportOptions.IncludeBackTranslations = true;
            taskContext.ExportOptions.IncludeTranslations     = true;
            taskContext.ExportOptions.CopySourceToTarget      = false;


            taskContext.LocalProjectFolder = newStudioProjectInfo.LocalProjectFolder;
            taskContext.WorkflowFolder     = taskContext.GetWorkflowPath();

            var workingProject = _projectAutomationService.GetProject(newStudioProject, null);

            workingProject.ProjectFiles.RemoveAll(a => a.TargetLanguage == workingProject.SourceLanguage.CultureInfo.Name);
            taskContext.Project          = workingProject;
            taskContext.FileBasedProject = newStudioProject;
            taskContext.ProjectFiles     = workingProject.ProjectFiles;

            foreach (var projectFile in taskContext.ProjectFiles)
            {
                projectFile.Selected = true;
                var fileData = GetFileData(languageFileData, localProjectFolder, projectFile);

                var tmpInputFile = Path.GetTempFileName();
                File.Move(tmpInputFile, tmpInputFile + ".sdlxliff");
                tmpInputFile = tmpInputFile + ".sdlxliff";

                var paragraphMap = GetParagraphMap(sdlxliffReader, projectFile.ProjectId, projectFile.FileId,
                                                   projectFile.Location, projectFile.TargetLanguage);
                AlignParagraphIds(fileData.Data, paragraphMap.Keys.ToList());

                var filePath = Path.Combine(taskContext.WorkingFolder, projectFile.Path.Trim('\\'));

                var externalFilePath = Path.Combine(filePath, projectFile.Name + ".xliff");
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }

                xliffWriter.WriteFile(fileData.Data, externalFilePath, true);

                var success = sdlxliffWriter.UpdateFile(fileData.Data, projectFile.Location, tmpInputFile, true);
                if (success)
                {
                    projectFile.Date                        = taskContext.DateTimeStamp;
                    projectFile.Action                      = action;
                    projectFile.WorkFlow                    = workFlow;
                    projectFile.Status                      = Enumerators.Status.Success;
                    projectFile.Report                      = string.Empty;
                    projectFile.ExternalFilePath            = externalFilePath;
                    projectFile.ConfirmationStatistics      = sdlxliffWriter.ConfirmationStatistics;
                    projectFile.TranslationOriginStatistics = sdlxliffWriter.TranslationOriginStatistics;
                }

                var activityFile = new ProjectFileActivity
                {
                    ProjectFileId               = projectFile.FileId,
                    ActivityId                  = Guid.NewGuid().ToString(),
                    Action                      = action,
                    WorkFlow                    = workFlow,
                    Status                      = success ? Enumerators.Status.Success : Enumerators.Status.Error,
                    Date                        = projectFile.Date,
                    Name                        = Path.GetFileName(projectFile.ExternalFilePath),
                    Path                        = Path.GetDirectoryName(projectFile.ExternalFilePath),
                    Report                      = string.Empty,
                    ProjectFile                 = projectFile,
                    ConfirmationStatistics      = projectFile.ConfirmationStatistics,
                    TranslationOriginStatistics = projectFile.TranslationOriginStatistics
                };

                projectFile.ProjectFileActivities.Add(activityFile);

                File.Copy(projectFile.Location, Path.Combine(filePath, projectFile.Name));
                File.Delete(projectFile.Location);

                File.Copy(tmpInputFile, projectFile.Location, true);
                File.Delete(tmpInputFile);
            }

            return(taskContext);
        }