예제 #1
0
        /// <summary>
        /// Extract the content from embedded resource file
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetResourceTextFile(string fileName)
        {
            try
            {
                telemetryTracker.StartTrackRequest("Reading embedded .xdt file");
                telemetryTracker.TrackEvent("Reading embedded .xdt file", null);

                var result = string.Empty;

                using (Stream stream = typeof(TermbaseDefinitionFile).Assembly.GetManifestResourceStream("Sdl.Community.ProjectTerms.Plugin.Resources." + fileName))
                {
                    using (StreamReader sr = new StreamReader(stream))
                    {
                        result = sr.ReadToEnd();
                    }
                }

                return(result);
            }
            catch (Exception e)
            {
                telemetryTracker.TrackException(new TermbaseDefinitionException(PluginResources.Error_GetResourceTextFile + e.Message));
                telemetryTracker.TrackTrace((new TermbaseDefinitionException(PluginResources.Error_GetResourceTextFile + e.Message)).StackTrace, Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Error);
                throw new TermbaseDefinitionException(PluginResources.Error_GetResourceTextFile + e.Message);
            }
        }
예제 #2
0
        public void ExtractBilingualContent(ProjectFile[] targetFiles)
        {
            try
            {
                telemetryTracker.StartTrackRequest("Extracting content from bilingual files");
                telemetryTracker.TrackEvent("Extracting content from bilingual files", null);

                foreach (var file in targetFiles)
                {
                    var converter = FileTypeManager.GetConverter(file.LocalFilePath, (sender, e) => { });
                    var extractor = new TextExtractionBilingualContentHandler();
                    converter.AddBilingualProcessor(new Sdl.FileTypeSupport.Framework.Core.Utilities.BilingualApi.BilingualContentHandlerAdapter(extractor));
                    converter.Parse();

                    for (int i = 0; i < extractor.SourceText.Count; i++)
                    {
                        if (string.IsNullOrWhiteSpace(extractor.SourceText[i]) || string.IsNullOrWhiteSpace(extractor.TargetText[i]))
                        {
                            continue;
                        }
                        AddItemToBilingualContentPair(extractor.SourceText[i], extractor.TargetText[i], file.Language.DisplayName);
                    }
                }
            }
            catch (Exception e)
            {
                telemetryTracker.TrackException(new ProjectTermsException(PluginResources.Error_ExtractContent + e.Message));
                telemetryTracker.TrackTrace((new ProjectTermsException(PluginResources.Error_ExtractContent + e.Message)).StackTrace, Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Error);
                throw new ProjectTermsException(PluginResources.Error_ExtractContent + e.Message);
            }
        }
예제 #3
0
        public void Save(string projectPath, IEnumerable <ITerm> terms, bool wordCloudFile = false)
        {
            try
            {
                telemetryTracker.StartTrackRequest("Saving the xml file");
                telemetryTracker.TrackEvent("Saving the xml file", null);

                XDocument doc = new XDocument();
                doc.Add(
                    new XElement("projectTerms",
                                 new XElement("terms",
                                              from term in terms
                                              select new XElement("term", new XAttribute("count", term.Occurrences), term.Text)))
                    );

                if (!wordCloudFile)
                {
                    var directoryPath = Path.GetDirectoryName(Utils.Utils.GetXMLFilePath(projectPath));
                    var cacheFile     = string.Empty;
                    if (!Directory.Exists(directoryPath))
                    {
                        Utils.Utils.CreateDirectory(directoryPath);
                        cacheFile = Utils.Utils.GetXMLFilePath(projectPath);
                    }
                    else
                    {
                        var fileName = Utils.Utils.GetExistedFileName(directoryPath);

                        if (fileName != "")
                        {
                            Utils.Utils.RemoveDirectoryFiles(directoryPath);
                            cacheFile = Path.Combine(projectPath + "\\tmp", fileName);
                        }
                        else
                        {
                            cacheFile = Utils.Utils.GetXMLFilePath(projectPath);
                        }
                    }


                    doc.Save(cacheFile);
                }
                else
                {
                    doc.Save(Utils.Utils.GetXMLFilePath(projectPath, true));
                }
            }
            catch (Exception e)
            {
                telemetryTracker.TrackException(new ProjectTermsException(PluginResources.Error_Save + e.Message));
                telemetryTracker.TrackTrace((new ProjectTermsException(PluginResources.Error_Save + e.Message)).StackTrace, Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Error);

                throw new ProjectTermsException(PluginResources.Error_Save + e.Message);
            }
        }
        private void UpdateExistedFile(Guid id)
        {
            try
            {
                telemetryTracker.StartTrackRequest("Updating xml file");
                telemetryTracker.TrackEvent("Updating xml file", null);

                var exitedFileName  = Utils.Utils.GetExistedFileName(Path.GetDirectoryName(Utils.Utils.GetXMLFilePath(Utils.Utils.GetProjectPath())));
                var existedFilePath = Path.Combine(Path.GetDirectoryName(Utils.Utils.GetXMLFilePath(Utils.Utils.GetProjectPath())), exitedFileName);
                var currentProject  = Utils.Utils.GetCurrentProject();
                currentProject.AddNewFileVersion(id, existedFilePath);
                var scan        = currentProject.RunAutomaticTask(new Guid[] { id }, AutomaticTaskTemplateIds.Scan);
                var convertTask = currentProject.RunAutomaticTask(new Guid[] { id }, AutomaticTaskTemplateIds.ConvertToTranslatableFormat);
                var targetFiles = currentProject.GetTargetLanguageFiles();
                foreach (var file in targetFiles)
                {
                    if (file.Name.Contains(Utils.Utils.GetCurrentProject().GetProjectInfo().Name))
                    {
                        File.Delete(file.LocalFilePath);
                    }
                }

                var sourceXmlFile = Utils.Utils.GetCurrentProject()?.GetSourceLanguageFiles()?
                                    .FirstOrDefault(x => x.Name.StartsWith(Path.GetFileNameWithoutExtension(Utils.Utils.GetProjectPath())));
                if (sourceXmlFile != null)
                {
                    foreach (var file in targetFiles)
                    {
                        if (file.Name.Contains(Utils.Utils.GetCurrentProject()?.GetProjectInfo()?.Name))
                        {
                            File.Copy(sourceXmlFile.LocalFilePath, file.LocalFilePath);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                telemetryTracker.TrackException(new ProjectTermsException(PluginResources.Error_UpdateExitedFile + e.Message));
                telemetryTracker.TrackTrace((new ProjectTermsException(PluginResources.Error_UpdateExitedFile + e.Message)).StackTrace, Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Error);
                throw new ProjectTermsException(PluginResources.Error_UpdateExitedFile + e.Message);
            }
        }
        /// <summary>
        /// Create a termbase and add it to Termbases file in sdl project.
        /// </summary>
        /// <param name="termbaseDefinitionPath"></param>
        /// <returns></returns>
        public ITermbase CreateTermbase(string termbaseDefinitionPath)
        {
            try
            {
                _telemetryTracker.StartTrackRequest("Creating termbase");
                _telemetryTracker.TrackEvent("Creating termbase");

                var termbases = ConnectToTermbaseLocalRepository();

                if (File.Exists(_termbasePath) && ExistsProjectTermbase())
                {
                    return(null);
                }
                var termbase = termbases.New(Path.GetFileNameWithoutExtension(_selectedFile.LocalFilePath), "Optional Description", termbaseDefinitionPath, _termbasePath);

                Utils.Utils.RemoveDirectory(Path.GetDirectoryName(termbaseDefinitionPath));
                return(termbase);
            }
            catch (Exception e)
            {
                _telemetryTracker.TrackException(new TermbaseGenerationException(PluginResources.Error_CreateTermbase + e.Message));
                _telemetryTracker.TrackTrace((new TermbaseGenerationException(PluginResources.Error_CreateTermbase + e.Message)).StackTrace, Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Error);
                throw new TermbaseGenerationException(PluginResources.Error_CreateTermbase + e.Message);
            }
        }
예제 #6
0
        private void Settings()
        {
            try
            {
                telemetryTracker.StartTrackRequest("Termbase settings");
                telemetryTracker.TrackEvent("Termbase settings", null);

                project      = SdlTradosStudio.Application.GetController <ProjectsController>().CurrentProject;
                selectedFile = SdlTradosStudio.Application.GetController <FilesController>().SelectedFiles.FirstOrDefault();
                termbasePath = Path.Combine(Path.GetTempPath() + "\\Tb", Path.GetFileNameWithoutExtension(selectedFile.LocalFilePath) + ".sdltb");
                if (!Directory.Exists(Path.GetDirectoryName(termbasePath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(termbasePath));
                }
            }
            catch (Exception e)
            {
                telemetryTracker.TrackException(new TermbaseGenerationException(PluginResources.Error_Settings + e.Message));
                telemetryTracker.TrackTrace((new TermbaseGenerationException(PluginResources.Error_Settings + e.Message)).StackTrace, Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Error);

                throw new TermbaseGenerationException(PluginResources.Error_Settings + e.Message);
            }
        }