private List <SearchResults> GetPreTranslationSearchResults(List <PreTranslateSegment> preTranslateList)
        {
            var resultsList = new List <SearchResults>();

            foreach (var preTranslate in preTranslateList)
            {
                var translation = new Segment(_languageDirection.TargetCulture);
                var newSeg      = preTranslate.TranslationUnit.SourceSegment.Duplicate();
                if (newSeg.HasTags)
                {
                    var tagPlacer = new DeepLTranslationProviderTagPlacer(newSeg);

                    translation = tagPlacer.GetTaggedSegment(preTranslate.PlainTranslation);
                    preTranslate.TranslationSegment = translation;
                }
                else
                {
                    translation.Add(preTranslate.PlainTranslation);
                }
                var searchResult = CreateSearchResult(newSeg, translation);
                var results      = new SearchResults
                {
                    SourceSegment = newSeg
                };
                results.Add(searchResult);
                resultsList.Add(results);
            }
            return(resultsList);
        }
Exemplo n.º 2
0
        public SearchResults SearchSegment(SearchSettings settings, Segment segment)
        {
            var translation = new Segment(_languageDirection.TargetCulture);
            var results     = new SearchResults
            {
                SourceSegment = segment.Duplicate()
            };

            try
            {
                // if there are match in tm the provider will not search the segment
                #region "Confirmation Level"
                if (!_options.ResendDrafts && _inputTu.ConfirmationLevel != ConfirmationLevel.Unspecified)
                {
                    translation.Add(PluginResources.TranslationLookupDraftNotResentMessage);
                    //later get these strings from resource file
                    results.Add(CreateSearchResult(segment, translation));
                    return(results);
                }
                var newseg = segment.Duplicate();
                if (newseg.HasTags)
                {
                    var tagPlacer      = new DeepLTranslationProviderTagPlacer(newseg);
                    var translatedText = LookupDeepl(tagPlacer.PreparedSourceText);
                    if (!string.IsNullOrEmpty(translatedText))
                    {
                        translation = tagPlacer.GetTaggedSegment(translatedText);

                        results.Add(CreateSearchResult(newseg, translation));
                        return(results);
                    }
                }
                else
                {
                    var sourcetext     = newseg.ToPlain();
                    var translatedText = LookupDeepl(sourcetext);
                    if (!string.IsNullOrEmpty(translatedText))
                    {
                        translation.Add(translatedText);

                        results.Add(CreateSearchResult(newseg, translation));
                        return(results);
                    }
                }
                #endregion
            }
            catch (Exception e)
            {
                Log.Logger.Error($"SearchSegment method: {e.Message}\n {e.StackTrace}");
            }
            return(results);
        }
Exemplo n.º 3
0
        private async Task <List <PreTranslateSegment> > PrepareTempData(List <PreTranslateSegment> preTranslateSegments)
        {
            try
            {
                for (var i = 0; i < preTranslateSegments.Count; i++)
                {
                    if (preTranslateSegments[i] != null)
                    {
                        string sourceText;
                        var    newSeg = preTranslateSegments[i].TranslationUnit.SourceSegment.Duplicate();

                        if (newSeg.HasTags)
                        {
                            var tagPlacer = new DeepLTranslationProviderTagPlacer(newSeg);
                            sourceText = tagPlacer.PreparedSourceText;
                        }
                        else
                        {
                            sourceText = newSeg.ToPlain();
                        }

                        preTranslateSegments[i].SourceText = sourceText;
                    }
                }

                var translator = new DeepLTranslationProviderConnecter(_options.ApiKey, _options.Formality);

                await Task.Run(() => Parallel.ForEach(preTranslateSegments, segment =>
                {
                    if (segment != null)
                    {
                        segment.PlainTranslation = translator.Translate(_languageDirection, segment.SourceText);
                    }
                })).ConfigureAwait(true);

                return(preTranslateSegments);
            }
            catch (Exception e)
            {
                _logger.Error($"{e.Message}\n {e.StackTrace}");
            }

            preTranslateSegments.ForEach(seg =>
            {
                if (seg.PlainTranslation == null)
                {
                    seg.PlainTranslation = string.Empty;
                }
            });
            return(preTranslateSegments);
        }
        public SearchResults SearchSegment(SearchSettings settings, Segment segment)
        {
            var translation = new Segment(_languageDirection.TargetCulture);
            var results     = new SearchResults()
            {
                SourceSegment = segment.Duplicate()
            };

            // if there are match in tm the provider will not search the segment
            #region "Confirmation Level"
            if (_inputTu.ConfirmationLevel != ConfirmationLevel.Unspecified)
            {
                translation.Add(PluginResources.TranslationLookupDraftNotResentMessage);
                //later get these strings from resource file
                results.Add(CreateSearchResult(segment, translation, segment.ToString()));
                return(results);
            }
            var newseg = segment.Duplicate();
            if (newseg.HasTags)
            {
                var tagPlacer      = new DeepLTranslationProviderTagPlacer(newseg);
                var translatedText = LookupDeepl(tagPlacer.PreparedSourceText);
                translation = tagPlacer.GetTaggedSegment(translatedText);
                //translation.Add(translatedText);

                results.Add(CreateSearchResult(newseg, translation, newseg.ToPlain()));
                return(results);
            }
            else
            {
                // TO BE IMPLEMENTED:
                // If there are tags in segment. We need to wait for information regarding how DeepL handels tags

                //for simple text
                var sourceLang = SourceLanguage.ToString();
                var targetLang = TargetLanguage.ToString();
                //a new seg avoids modifying the current segment object


                var sourcetext = newseg.ToPlain();

                var translatedText = LookupDeepl(sourcetext);
                translation.Add(translatedText);

                results.Add(CreateSearchResult(newseg, translation, newseg.ToPlain()));
                return(results);
            }

            #endregion
        }
Exemplo n.º 5
0
        public SearchResults SearchSegment(SearchSettings settings, Segment segment)
        {
            var translation = new Segment(_languageDirection.TargetCulture);
            var results     = new SearchResults
            {
                SourceSegment = segment.Duplicate()
            };

            try
            {
                var newseg = segment.Duplicate();
                if (newseg.HasTags && !_options.SendPlainText)
                {
                    var tagPlacer      = new DeepLTranslationProviderTagPlacer(newseg);
                    var translatedText = LookupDeepL(tagPlacer.PreparedSourceText);
                    if (!string.IsNullOrEmpty(translatedText))
                    {
                        translation = tagPlacer.GetTaggedSegment(translatedText);

                        results.Add(CreateSearchResult(newseg, translation));
                        return(results);
                    }
                }
                else
                {
                    var sourcetext = newseg.ToPlain();

                    var translatedText = LookupDeepL(sourcetext);
                    if (!string.IsNullOrEmpty(translatedText))
                    {
                        translation.Add(translatedText);

                        results.Add(CreateSearchResult(newseg, translation));
                        return(results);
                    }
                }
            }
            catch (Exception e)
            {
                _logger.Error($"SearchSegment method: {e.Message}\n {e.StackTrace}");
                throw;
            }
            return(results);
        }
        private async Task <List <PreTranslateSegment> > PrepareTempData(List <PreTranslateSegment> preTranslatesegments)
        {
            try
            {
                for (var i = 0; i < preTranslatesegments.Count; i++)
                {
                    if (preTranslatesegments[i] != null)
                    {
                        string sourceText;
                        var    newseg = preTranslatesegments[i].TranslationUnit.SourceSegment.Duplicate();

                        if (newseg.HasTags)
                        {
                            var tagPlacer = new DeepLTranslationProviderTagPlacer(newseg);
                            sourceText = tagPlacer.PreparedSourceText;
                        }
                        else
                        {
                            sourceText = newseg.ToPlain();
                        }

                        preTranslatesegments[i].SourceText = sourceText;
                    }
                }


                var translator = new DeepLTranslationProviderConnecter(_options.ApiKey, _options.Identifier);

                await Task.Run(() => Parallel.ForEach(preTranslatesegments, segment =>
                {
                    if (segment != null)
                    {
                        segment.PlainTranslation = translator.Translate(_languageDirection, segment.SourceText);
                    }
                })).ConfigureAwait(true);

                return(preTranslatesegments);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(preTranslatesegments);
        }
        private List <SearchResults> GetPreTranslationSearchResults(List <PreTranslateSegment> preTranslateList)
        {
            var resultsList = new List <SearchResults>(preTranslateList.Capacity);

            for (int i = 0; i < resultsList.Capacity; i++)
            {
                resultsList.Add(null);
            }

            foreach (var preTranslate in preTranslateList)
            {
                if (preTranslate != null)
                {
                    var translation = new Segment(_languageDirection.TargetCulture);
                    var newSeg      = preTranslate.TranslationUnit.SourceSegment.Duplicate();
                    if (newSeg.HasTags)
                    {
                        var tagPlacer = new DeepLTranslationProviderTagPlacer(newSeg);

                        translation = tagPlacer.GetTaggedSegment(preTranslate.PlainTranslation);
                        preTranslate.TranslationSegment = translation;
                    }
                    else
                    {
                        translation.Add(preTranslate.PlainTranslation);
                    }

                    var searchResult = CreateSearchResult(newSeg, translation);
                    var results      = new SearchResults
                    {
                        SourceSegment = newSeg
                    };
                    results.Add(searchResult);

                    var index = preTranslateList.IndexOf(preTranslate);
                    resultsList.RemoveAt(index);
                    resultsList.Insert(index, results);
                }
            }

            return(resultsList);
        }
Exemplo n.º 8
0
        public SearchResults SearchSegment(SearchSettings settings, Segment segment)
        {
            var translation = new Segment(_languageDirection.TargetCulture);
            var results     = new SearchResults()
            {
                SourceSegment = segment.Duplicate()
            };

            // if there are match in tm the provider will not search the segment
            #region "Confirmation Level"
            if (!_options.ResendDrafts && _inputTu.ConfirmationLevel != ConfirmationLevel.Unspecified)
            {
                translation.Add(PluginResources.TranslationLookupDraftNotResentMessage);
                //later get these strings from resource file
                results.Add(CreateSearchResult(segment, translation, segment.ToString()));
                return(results);
            }
            var newseg = segment.Duplicate();
            if (newseg.HasTags)
            {
                var tagPlacer      = new DeepLTranslationProviderTagPlacer(newseg);
                var translatedText = LookupDeepl(tagPlacer.PreparedSourceText);
                translation = tagPlacer.GetTaggedSegment(translatedText);

                results.Add(CreateSearchResult(newseg, translation, newseg.ToPlain()));
                return(results);
            }
            else
            {
                var sourcetext = newseg.ToPlain();

                var translatedText = LookupDeepl(sourcetext);
                translation.Add(translatedText);

                results.Add(CreateSearchResult(newseg, translation, newseg.ToPlain()));
                return(results);
            }

            #endregion
        }