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); } }
/// <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); } }
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); } }
public string Translate(LanguagePair languageDirection, string sourcetext) { const string tagOption = @"xml"; var targetLanguage = languageDirection.TargetCulture.TwoLetterISOLanguageName; var url = string.Format("https://api.deepl.com/v1/translate?text={0}&target_lang={1}&tag_handling={2}&auth_key={3}" , sourcetext, targetLanguage, tagOption, ApiKey); var translatedText = string.Empty; using (var webClient = new WebClient()) { webClient.Encoding = Encoding.UTF8; try { var response = webClient.DownloadString(url); var translatedObject = JsonConvert.DeserializeObject <TranslationResponse>(response); if (translatedObject != null) { translatedText = translatedObject.Translations[0].Text; } } catch (WebException e) //will come back 400 bad request if there is a problem { var eReason = Helpers.ProcessWebException(e); _telemetryTracker.TrackException(e); _telemetryTracker.TrackTrace(e.StackTrace, Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Error); throw new Exception(eReason); } } return(translatedText); }
public string Translate(LanguagePair languageDirection, string sourcetext) { const string tagOption = @"xml"; var targetLanguage = languageDirection.TargetCulture.TwoLetterISOLanguageName; var translatedText = string.Empty; try { var client = new RestClient(@"https://api.deepl.com/v1"); var request = new RestRequest("translate", Method.POST); request.AddParameter("text", sourcetext); request.AddParameter("target_lang", targetLanguage); request.AddParameter("tag_handling", tagOption); request.AddParameter("split_sentences", 0); request.AddParameter("auth_key", ApiKey); var response = client.Execute(request).Content; var translatedObject = JsonConvert.DeserializeObject <TranslationResponse>(response); if (translatedObject != null) { translatedText = translatedObject.Translations[0].Text; } } catch (WebException e) { var eReason = Helpers.ProcessWebException(e); _telemetryTracker.TrackException(e); _telemetryTracker.TrackTrace(e.StackTrace, Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Error); throw new Exception(eReason); } return(translatedText); }
public string Translate(LanguagePair languageDirection, string sourcetext) { const string tagOption = @"xml"; var targetLanguage = languageDirection.TargetCulture.TwoLetterISOLanguageName; var sourceLanguage = languageDirection.SourceCulture.TwoLetterISOLanguageName; var translatedText = string.Empty; try { var client = new RestClient(@"https://api.deepl.com/v1"); var request = new RestRequest("translate", Method.POST); //search for words like this <word> var rgx = new Regex("(\\<\\w+[üäåëöøßşÿÄÅÆĞ]*[^\\d\\W\\\\/\\\\]+\\>)"); var words = rgx.Matches(sourcetext); if (words.Count > 0) { sourcetext = ReplaceCharacters(sourcetext, words); } //request.AddParameter("auth_key", ApiKey); //request.AddParameter("source_lang", sourceLanguage); //request.AddParameter("target_lang", targetLanguage); //request.AddParameter("text", sourcetext); request.AddParameter("text", sourcetext); request.AddParameter("source_lang", sourceLanguage); request.AddParameter("target_lang", targetLanguage); //adding this resolve line breaks issue and missing ##login## request.AddParameter("preserve_formatting", 1); //tag handling cause issues on uppercase words request.AddParameter("tag_handling", tagOption); //if we add this the formattiong is not right //request.AddParameter("split_sentences", 0); request.AddParameter("auth_key", ApiKey); var response = client.Execute(request).Content; var translatedObject = JsonConvert.DeserializeObject <TranslationResponse>(response); if (translatedObject != null) { translatedText = translatedObject.Translations[0].Text; translatedText = HttpUtility.HtmlDecode(translatedText); } } catch (WebException e) { var eReason = Helpers.ProcessWebException(e); _telemetryTracker.TrackException(e); _telemetryTracker.TrackTrace(e.StackTrace, Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Error); throw new Exception(eReason); } return(translatedText); }
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); } }
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); } }