Exemple #1
0
        private void ImportLanguagePairTM(LanguagePair pair, FileBasedProject project)
        {
            if (pair.HasTm)
            {
                if (pair.StarTranslationMemoryMetadatas.Count > 0)
                {
                    var newTmPath = Path.Combine(Path.GetDirectoryName(project.FilePath), Path.GetFileName(pair.TmPath));
                    var importer  = new TransitTmImporter(pair, _fileTypeManager, newTmPath);

                    foreach (var tm in pair.StarTranslationMemoryMetadatas)
                    {
                        importer.ImportStarTransitTm(tm.TargetFile);
                    }
                    _tmConfig.Entries.Add(new TranslationProviderCascadeEntry(importer.GetTranslationProviderReference(), true, true, true));
                }

                // Create separate TM for each TM file on which user set penalty. The penalty is applied on top of any penalty that might be applied by the translation provider itself.
                // (the name of the new TM will be the same with the one from StarTransit package)
                foreach (var item in _penaltiesTmsList)
                {
                    var tpReference = CreateTpReference(item, pair, project);
                    _tmConfig.Entries.Add(new TranslationProviderCascadeEntry(tpReference, true, true, true, item.TMPenalty));
                }

                //If the user requests it, create a separate TM for the Machine Translation coming from Transit.
                foreach (var item in _machineTransList)
                {
                    var tpReference = CreateTpReference(item, pair, project);

                    //It should have a penalty set by default, otherwise it will be used for pretranslation and later added to the main TM when updating main TM, and we want to avoid that.
                    _tmConfig.Entries.Add(new TranslationProviderCascadeEntry(tpReference, true, true, true, 1));
                }
            }
        }
Exemple #2
0
        private void ImportLanguagePairTm(LanguagePair pair, IProject project, PackageModel package)
        {
            if (!pair.HasTm || string.IsNullOrEmpty(pair.TmPath))
            {
                return;
            }
            if (pair.StarTranslationMemoryMetadatas != null && pair.StarTranslationMemoryMetadatas.Any())
            {
                var localProjectFolder = project?.GetProjectInfo()?.LocalProjectFolder;
                if (localProjectFolder != null)
                {
                    var newTmPath = Path.Combine(localProjectFolder, Path.GetFileName(pair.TmPath));
                    var importer  = new TransitTmImporter(pair, newTmPath, null);

                    importer.ImportStarTransitTm(pair.StarTranslationMemoryMetadatas, package);
                    var providerRef = importer.GetTranslationProviderReference(newTmPath, pair);
                    _logger.Info($"-->Import lang pair Provider Reference:{providerRef?.Uri}");
                    if (providerRef == null)
                    {
                        return;
                    }
                    //var test = new TranslationProviderCascadeEntry(providerRef, true, true, true);
                    _tmConfig.Entries.Add(new TranslationProviderCascadeEntry(providerRef, true, true, true));
                }
            }

            CreateSeparateTms(pair, project, package);
        }
Exemple #3
0
        private MessageModel ImportTms(PackageModel package, LanguagePair languagePair)
        {
            if (!languagePair.HasTm || string.IsNullOrEmpty(package.Location))
            {
                return(null);                //TODO: investigate what we need to return
            }
            foreach (var metadataTransitFile in languagePair.SelectedTranslationMemoryMetadatas)
            {
                var tmDescription = $"{metadataTransitFile.Name} Translation Memory";
                if (languagePair.CreateNewTm)
                {
                    //Only for create new tm option we need to check for duplicated name, for Browse existing tm we'll import into selected tm
                    metadataTransitFile.LocalTmCreationPath = GetTmPathForDuplicatedName(metadataTransitFile.LocalTmCreationPath, _tmConfig.Entries);
                }

                var importer = new TransitTmImporter(languagePair, tmDescription, metadataTransitFile.LocalTmCreationPath, _eventAggregatorService);
                importer.ImportStarTransitTm(metadataTransitFile.TransitTmsSourceFilesPath,
                                             metadataTransitFile.TransitTmsTargeteFilesPath, languagePair.TargetLanguage, package);

                var providerRef = importer.GetTranslationProviderReference();
                _logger.Info($"-->Import lang pair Provider Reference:{providerRef?.Uri}");

                if (providerRef == null)
                {
                    return(null);
                }
                var entryExists =
                    _tmConfig.Entries.Any(e => e.MainTranslationProvider.Uri.Equals(providerRef.Uri));
                if (!entryExists)
                {
                    _tmConfig.Entries.Add(new TranslationProviderCascadeEntry(providerRef, true, true, true, metadataTransitFile.TmPenalty));
                }
            }
            return(null);
        }
Exemple #4
0
        public MessageModel CreateProject(PackageModel package)
        {
            var penaltiesTmsList = new List <StarTranslationMemoryMetadata>();
            var target           = GetTargetLanguages(package.LanguagePairs);

            var projectInfo = new ProjectInfo
            {
                Name = package.Name,
                LocalProjectFolder = package.Location,
                SourceLanguage     = new Language(package.LanguagePairs[0].SourceLanguage),
                TargetLanguages    = target,
                DueDate            = package.DueDate
            };

            var newProject = new FileBasedProject(projectInfo,
                                                  new ProjectTemplateReference(package.ProjectTemplate.Uri));

            if (package.Customer != null)
            {
                newProject.SetCustomer(package.Customer);
            }

            //Add StarTransit package source files. The same on all language pairs
            ProjectFile[] sourceProjectFiles = newProject.AddFiles(package.LanguagePairs[0].SourceFile.ToArray());

            //set the file role(user to display project details in Studio view)
            var sourceFilesIds = newProject.GetSourceLanguageFiles().GetIds();

            newProject.SetFileRole(sourceFilesIds, FileRole.Translatable);

            var tmConfig        = newProject.GetTranslationProviderConfiguration();
            var fileTypeManager = DefaultFileTypeManager.CreateInstance(true);

            var targetProjectFiles = new List <ProjectFile>();

            foreach (var pair in package.LanguagePairs)
            {
                // Separate all items from package.TMPenalties(files that are having penalties set)
                // which are found in pair.StarTranslationMemoryMetadatas
                foreach (var starTMMetadata in pair.StarTranslationMemoryMetadatas)
                {
                    if (package.TMPenalties != null)
                    {
                        if (package.TMPenalties.Any(t => t.Key.Equals(starTMMetadata.TargetFile)))
                        {
                            starTMMetadata.TMPenalty = package.TMPenalties.FirstOrDefault(t => t.Key.Equals(starTMMetadata.TargetFile)).Value;
                            penaltiesTmsList.Add(starTMMetadata);
                        }
                    }
                }

                // Remove found items from pair.StarTranslationMemoryMetadatas (the remained ones are those which does not have penalties set on them)
                foreach (var item in penaltiesTmsList)
                {
                    pair.StarTranslationMemoryMetadatas.Remove(item);
                }

                // Create one TM (that has the name equals with the project name)
                // with the TM files from pair.StarTranslationMemoryMetadatas (the onew without penalties set by the user)
                targetProjectFiles.Clear();

                // Import language pair TM if any
                if (pair.HasTm)
                {
                    if (pair.StarTranslationMemoryMetadatas.Count > 0)
                    {
                        var importer = new TransitTmImporter(pair.SourceLanguage,
                                                             pair.TargetLanguage,
                                                             pair.CreateNewTm,
                                                             fileTypeManager,
                                                             Path.Combine(Path.GetDirectoryName(newProject.FilePath), Path.GetFileName(pair.TmPath)));
                        foreach (var tm in pair.StarTranslationMemoryMetadatas)
                        {
                            importer.ImportStarTransitTm(tm.TargetFile);
                        }

                        tmConfig.Entries.Add(new TranslationProviderCascadeEntry(importer.GetTranslationProviderReference(),
                                                                                 true,
                                                                                 true,
                                                                                 true));
                    }

                    // Create separate TM for each TM file on which user set penalty
                    // (the name of the new TM will be the same with the one from StarTransit package)
                    foreach (var item in penaltiesTmsList)
                    {
                        var tmWithPenaltyImporter = new TransitTmImporter(
                            Path.GetDirectoryName(newProject.FilePath),
                            pair.SourceLanguage,
                            pair.TargetLanguage,
                            Path.GetFileName(item.TargetFile),
                            fileTypeManager);

                        tmWithPenaltyImporter.ImportStarTransitTm(item.TargetFile);

                        tmConfig.Entries.Add(new TranslationProviderCascadeEntry(
                                                 new TranslationProviderReference(tmWithPenaltyImporter.TMFilePath),
                                                 true,
                                                 true,
                                                 true,
                                                 item.TMPenalty));
                    }
                }
                if (!pair.TargetFile.Any() || pair.TargetFile.Count == 0)
                {
                    var messageModel = new MessageModel
                    {
                        IsProjectCreated = false,
                        Message          = "Project was not created correctly because no target files were found in the package!",
                        Title            = "Informative message"
                    };
                    return(messageModel);
                }
                targetProjectFiles.AddRange(newProject.AddFiles(pair.TargetFile.ToArray()));

                //update tm settings
                UpdateTmSettings(newProject);
                newProject.UpdateTranslationProviderConfiguration(tmConfig);

                newProject.RunAutomaticTask(targetProjectFiles.GetIds(), AutomaticTaskTemplateIds.Scan);
                var taskSequence = newProject.RunAutomaticTasks(targetProjectFiles.GetIds(), new[]
                {
                    AutomaticTaskTemplateIds.ConvertToTranslatableFormat,
                    AutomaticTaskTemplateIds.CopyToTargetLanguages,
                    AutomaticTaskTemplateIds.PerfectMatch,
                    AutomaticTaskTemplateIds.PreTranslateFiles,
                    AutomaticTaskTemplateIds.AnalyzeFiles,
                    AutomaticTaskTemplateIds.UpdateMainTranslationMemories
                });

                if (taskSequence.Status.Equals(TaskStatus.Failed))
                {
                    var messageModel = new MessageModel
                    {
                        IsProjectCreated = false,
                        Message          = "Project could not be created.Error occured while running automatic tasks!",
                        Title            = "Informative message"
                    };
                    return(messageModel);
                }

                newProject.Save();
            }
            Controller.RefreshProjects();
            if (Directory.Exists(Path.GetDirectoryName(newProject.FilePath)))
            {
                CreateMetadataFolder(package.Location, package.PathToPrjFile);

                Controller.RefreshProjects();
            }
            return(null);
        }