Exemple #1
0
        //Whenever doc changes, start translating the segments and caching translations
        private static void DocChanged(object sender, DocumentEventArgs e)
        {
            if (e.Document == null)
            {
                return;
            }

            OpusCatProvider.UpdateSegmentHandler(e.Document);
        }
Exemple #2
0
        private static void UpdateSegmentHandler(Document doc)
#endif
        {
            //This method may be fired through docChanged event or through settings change.

            OpusCatProvider.ClearSegmentHandlers();

            OpusCatProvider.activeDocument = doc;

            var project     = doc.Project;
            var projectInfo = project.GetProjectInfo();

            LanguageDirection langDir;

            //Check whether document contains files
            if (doc.Files.Any())
            {
                //only files of same language can be merged, so taking the langdir of first file is enough
                langDir = doc.Files.First().GetLanguageDirection();
            }
            else
            {
                return;
            }

            var activeOpusCatOptions = OpusCatProvider.GetProjectOpusCatOptions(project, langDir);

            if (activeOpusCatOptions != null)
            {
                if (activeOpusCatOptions.Any(x => x.pregenerateMt))
                {
                    //The previous solution for pregeneration was to start translating the
                    //whole document as soon as the doc changes. This has a problem:
                    //if you have a massive document, just opening the document will cause a massive
                    //load on the translation service.
                    //So instead this was changed to add a segment changed handler which order only a certain
                    //amount on new translations for the next n segments whenever segment changes.
                    //Previous solution is provided below, commented out.

                    //Assign the handler to field to make it possible to remove it later
                    if (!OpusCatProvider.activeSegmentHandlers.ContainsKey(doc as Document))
                    {
                        OpusCatProvider.activeSegmentHandlers[doc as Document] = new List <EventHandler>();
                    }

                    var handler = new EventHandler((x, y) => segmentChanged(langDir, x, y));
                    OpusCatProvider.activeSegmentHandlers[doc as Document].Add(handler);

                    doc.ActiveSegmentChanged += handler;
                }
            }
            else
            {
                return;
            }
        }
Exemple #3
0
        internal static void UpdateSegmentHandler()
        {
            EditorController editorController = SdlTradosStudio.Application.GetController <EditorController>();
            var activeDoc = editorController.ActiveDocument;

            if (activeDoc != null)
            {
                OpusCatProvider.UpdateSegmentHandler(activeDoc);
            }
        }
Exemple #4
0
        public ITranslationProvider CreateTranslationProvider(Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore)
        {
            if (!SupportsTranslationProviderUri(translationProviderUri))
            {
                throw new Exception("Cannot handle URI.");
            }

            OpusCatProvider tp = new OpusCatProvider(new OpusCatOptions(translationProviderUri));

            return(tp);
        }
Exemple #5
0
        /// <summary>
        /// Show the plug-in settings form when the user is adding the translation provider plug-in
        /// through the GUI of SDL Trados Studio
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="languagePairs"></param>
        /// <param name="credentialStore"></param>
        /// <returns></returns>
        #region "Browse"
        public ITranslationProvider[] Browse(IWin32Window owner, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore)
        {
            OpusCatOptionsFormWPF dialog = new OpusCatOptionsFormWPF(new OpusCatOptions(), languagePairs, credentialStore);

            if (dialog.ShowDialog(owner) == DialogResult.OK)
            {
                OpusCatProvider testProvider = new OpusCatProvider(dialog.Options, credentialStore);
                return(new ITranslationProvider[] { testProvider });
            }
            return(null);
        }
        /// <summary>
        /// Instantiates the variables and fills the list file content into
        /// a Dictionary collection object.
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="languages"></param>
        #region "ListTranslationProviderLanguageDirection"
        public OpusCatProviderLanguageDirection(OpusCatProvider provider, LanguagePair languages)
        {
            #region "Instantiate"

            _provider          = provider;
            _languageDirection = languages;
            _options           = _provider.Options;

            _visitor = new OpusCatProviderElementVisitor();

            var sourceCode = this._languageDirection.SourceCulture.TwoLetterISOLanguageName;
            var targetCode = this._languageDirection.TargetCulture.TwoLetterISOLanguageName;
            this.langpair = $"{sourceCode}-{targetCode}";

            #endregion
        }
Exemple #7
0
        /// <summary>
        /// If the plug-in settings can be changed by the user,
        /// SDL Trados Studio will display a Settings button.
        /// By clicking this button, users raise the plug-in user interface,
        /// in which they can modify any applicable settings.
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="translationProvider"></param>
        /// <param name="languagePairs"></param>
        /// <param name="credentialStore"></param>
        /// <returns></returns>
        #region "Edit"
        public bool Edit(IWin32Window owner, ITranslationProvider translationProvider, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore)
        {
            OpusCatProvider editProvider = translationProvider as OpusCatProvider;

            if (editProvider == null)
            {
                return(false);
            }

            OpusCatOptionsFormWPF dialog = new OpusCatOptionsFormWPF(editProvider.Options, languagePairs, credentialStore);

            if (dialog.ShowDialog(owner) == DialogResult.OK)
            {
                return(true);
            }

            return(false);
        }
Exemple #8
0
        public OpusCatProvider(OpusCatOptions options)
        {
            //TODO: add some kind of throttling here or to Helper to prevent the service being overwhelmed by requests.
            //Just keep a count of open connections and prevent connections when there are more than 100 or so.
            Options = options;

            //If we create a provider with the pregenerate on, add a doc change handler to start preordering
            //MT when doc is changed
            if (options.pregenerateMt)
            {
                EditorController editorController = SdlTradosStudio.Application.GetController <EditorController>();
                //This should ensure the handler is only attached once, by always removing a possible previously
                //added handler before adding the new one
                editorController.ActiveDocumentChanged -= OpusCatProvider.DocChanged;
                editorController.ActiveDocumentChanged += OpusCatProvider.DocChanged;

                //If a document is open, check if the segment change handler should be added
                OpusCatProvider.UpdateSegmentHandler();
            }
        }
Exemple #9
0
        public OpusCatProvider(OpusCatOptions options, ITranslationProviderCredentialStore credentialStore)
        {
            Options = options;

            //If we create a provider with the pregenerate on, add a doc change handler to start preordering
            //MT when doc is changed
            if (options.pregenerateMt && options.opusCatSource == OpusCatOptions.OpusCatSource.OpusCatMtEngine)
            {
                EditorController editorController = SdlTradosStudio.Application.GetController <EditorController>();
                //This should ensure the handler is only attached once, by always removing a possible previously
                //added handler before adding the new one
                editorController.ActiveDocumentChanged -= OpusCatProvider.DocChanged;
                editorController.ActiveDocumentChanged += OpusCatProvider.DocChanged;

                //If a document is open, check if the segment change handler should be added
                OpusCatProvider.UpdateSegmentHandler();
            }

            if (this.Options.opusCatSource == OpusCatOptions.OpusCatSource.Elg)
            {
                OpusCatProvider.ElgConnection = new ElgServiceConnection(new TradosElgCredentialWrapper(credentialStore));
            }
        }
Exemple #10
0
        private static void segmentChanged(LanguageDirection langDir, object sender, EventArgs e)
        {
            var doc = (Document)sender;

            //There are some "segments" the Trados editor view which are not proper segments, like
            //the start of document tag
            if (doc.ActiveSegmentPair == null)
            {
                return;
            }
            var visitor = new OpusCatMarkupDataVisitor();

            var activeOpusCatOptions = OpusCatProvider.GetProjectOpusCatOptions(doc.Project, langDir);

            IEnumerable <OpusCatOptions> activeOpusCatOptionsWithPregenerate;

            if (activeOpusCatOptions == null)
            {
                activeOpusCatOptionsWithPregenerate = null;
            }
            else
            {
                activeOpusCatOptionsWithPregenerate = activeOpusCatOptions.Where(x => x.pregenerateMt);
            }
            //If there is no active OPUS CAT provider, unsubscribe this handler (there's probably no event in Trados
            //API for removing a translation provider from a project, so this is the only way to unsubscribe
            //after translation provider has been removed.
            if (activeOpusCatOptionsWithPregenerate == null || !activeOpusCatOptionsWithPregenerate.Any())
            {
                OpusCatProvider.ClearSegmentHandlers();
                return;
            }

            var sourceSegmentTexts = new List <string>();

            var nextSegmentPairs = doc.SegmentPairs.SkipWhile(x =>
                                                              !(x.Properties.Id == doc.ActiveSegmentPair.Properties.Id &&
                                                                x.GetParagraphUnitProperties().ParagraphUnitId == doc.ActiveSegmentPair.GetParagraphUnitProperties().ParagraphUnitId));

            var segmentsNeeded = activeOpusCatOptionsWithPregenerate.Max(x => x.pregenerateSegmentCount);

            foreach (var segmentPair in nextSegmentPairs)
            {
                if (sourceSegmentTexts.Count == segmentsNeeded)
                {
                    break;
                }

                //Also preorder translations for Draft segments, since quite often there will be draft content
                //provided in segments where having MT is still desirable. This could also be an option.
                if (segmentPair.Properties.ConfirmationLevel == Sdl.Core.Globalization.ConfirmationLevel.Unspecified ||
                    segmentPair.Properties.ConfirmationLevel == Sdl.Core.Globalization.ConfirmationLevel.Draft)
                {
                    visitor.Reset();
                    segmentPair.Source.AcceptVisitor(visitor);
                    var sourceText = visitor.PlainText;
                    sourceSegmentTexts.Add(sourceText);
                }
            }

            var sourceCode = langDir.SourceLanguage.CultureInfo.TwoLetterISOLanguageName;
            var targetCode = langDir.TargetLanguage.CultureInfo.TwoLetterISOLanguageName;

            foreach (var options in activeOpusCatOptionsWithPregenerate)
            {
                //The preorder method doesn't wait for the translation, so the requests return quicker
                var sourceSegmentTextsNeeded = sourceSegmentTexts.Take(options.pregenerateSegmentCount).ToList();
                OpusCatMTServiceHelper.PreOrderBatch(options, sourceSegmentTextsNeeded, sourceCode, targetCode, options.modelTag);
            }
        }