This class is used to hold the provider plug-in settings. All settings are automatically stored in a URI.
Example #1
0
        private string LookupMST(string sourcetext, MtTranslationOptions options, string format)
        {
            string catId = "";

            if (options.UseCatID)
            {
                catId = _options.CatID;//only use specific category ID if the option is selected
            }
            string sourcelang = _languageDirection.SourceCulture.ToString();
            string targetlang = _languageDirection.TargetCulture.ToString();

            //instantiate ApiConnecter if necessary
            if (mstConnect == null)
            {
                mstConnect = new MstTranslateConnect.ApiConnecter(_options);
            }
            else
            {
                mstConnect.resetCrd(options.ClientID, options.ClientSecret); //reset key in case it has been changed in dialog since GtApiConnecter was instantiated
            }

            string translatedText = mstConnect.Translate(sourcelang, targetlang, sourcetext, catId, format);

            return(translatedText);
        }
        private void UpdateDialog()
        {
            showcredsloc             = groupBoxMT.Location; //holds our location of where to place the group box we are showing
            textApiKey.Text          = Options.apiKey;
            txtClientId.Text         = Options.ClientID;
            txtClientSecret.Text     = Options.ClientSecret;
            chkSaveKey.Checked       = Options.persistGoogleKey;
            chkSaveCred.Checked      = Options.persistMicrosoftCreds;
            chkPlainTextOnly.Checked = Options.SendPlainTextOnly;
            comboProvider.Text       = MtTranslationOptions.GetProviderTypeDescription(Options.SelectedProvider);
            chkCatId.Checked         = Options.UseCatID;
            txtCatId.Text            = Options.CatID;
            chkResendDrafts.Checked  = Options.ResendDrafts;
            chkUsePreEdit.Checked    = Options.UsePreEdit;
            chkUsePostEdit.Checked   = Options.UsePostEdit;
            txtPreEditFileName.Text  = Options.PreLookupFilename;
            txtPostEditFileName.Text = Options.PostLookupFilename;


            //enable/disable controls
            groupBoxPostedit.Enabled = chkUsePostEdit.Checked;
            groupBoxPreedit.Enabled  = chkUsePreEdit.Checked;
            txtCatId.Enabled         = chkCatId.Checked;

            this.Icon = MtProviderConfDialogResources.form_Icon;

            ShowCredBox();
            try
            {
                LoadResources();
            }
            catch { }
        }
        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (!ValidateForm())
            {
                return;
            }

            Options.apiKey                = textApiKey.Text;
            Options.ClientID              = txtClientId.Text;
            Options.ClientSecret          = txtClientSecret.Text;
            Options.persistGoogleKey      = chkSaveKey.Checked;
            Options.persistMicrosoftCreds = chkSaveCred.Checked;
            Options.SendPlainTextOnly     = chkPlainTextOnly.Checked;
            Options.SelectedProvider      = MtTranslationOptions.GetProviderType(comboProvider.Text);
            Options.UseCatID              = chkCatId.Checked;
            Options.CatID              = txtCatId.Text;
            Options.ResendDrafts       = chkResendDrafts.Checked;
            Options.UsePreEdit         = chkUsePreEdit.Checked;
            Options.UsePostEdit        = chkUsePostEdit.Checked;
            Options.PreLookupFilename  = txtPreEditFileName.Text;
            Options.PostLookupFilename = txtPostEditFileName.Text;

            this.DialogResult = DialogResult.OK;
            this.Close(); //dispose????
        }
        /// <summary>
        /// Used for displaying the plug-in info such as the plug-in name,
        /// tooltip, and icon.
        /// </summary>
        /// <param name="translationProviderUri"></param>
        /// <param name="translationProviderState"></param>
        /// <returns></returns>
        #region "GetDisplayInfo"
        public TranslationProviderDisplayInfo GetDisplayInfo(Uri translationProviderUri, string translationProviderState)
        {
            TranslationProviderDisplayInfo info    = new TranslationProviderDisplayInfo();
            MtTranslationOptions           options = new MtTranslationOptions(translationProviderUri);

            info.TranslationProviderIcon = PluginResources.my_icon;

            if (options.SelectedProvider == MtTranslationOptions.ProviderType.GoogleTranslate)
            {
                info.Name              = PluginResources.Google_NiceName;
                info.TooltipText       = PluginResources.Google_Tooltip;
                info.SearchResultImage = PluginResources.my_image;
            }
            else if (options.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
            {
                info.Name              = PluginResources.Microsoft_NiceName;
                info.TooltipText       = PluginResources.Microsoft_Tooltip;
                info.SearchResultImage = PluginResources.microsoft_image;
            }
            else
            {
                info.Name        = PluginResources.Plugin_NiceName;
                info.TooltipText = PluginResources.Plugin_Tooltip;
            }
            return(info);
        }
        public ITranslationProvider CreateTranslationProvider(Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore)
        {
            #region "CheckHandlesUri"
            if (!SupportsTranslationProviderUri(translationProviderUri))
            {
                throw new Exception(PluginResources.UriNotSupportedMessage);
            }
            #endregion

            //create options class based on URI passed to the method
            MtTranslationOptions loadOptions = new MtTranslationOptions(translationProviderUri);

            //start with MT...check if we are using MT
            if (loadOptions.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
            {
                Uri myUri = new Uri("mtenhancedprovidermst:///");
                if (credentialStore.GetCredential(myUri) != null)
                {
                    bool credPersists = credentialStore.GetCredential(myUri).Persist;
                    TranslationProviderCredential cred = new TranslationProviderCredential("", credPersists); //will this work??

                    cred = credentialStore.GetCredential(myUri);                                              //if credential is there then just get it

                    GenericCredentials cred2 = new GenericCredentials(cred.Credential);                       //convert to generic credentials
                    //add creds to options
                    loadOptions.ClientID     = cred2.UserName;
                    loadOptions.ClientSecret = cred2.Password;
                }
                else
                {
                    throw new TranslationProviderAuthenticationException();
                }
            }
            else //if we are using Google as the provider need to get API key
            {
                Uri myUri = new Uri("mtenhancedprovidergt:///");
                if (credentialStore.GetCredential(myUri) != null)
                {
                    bool credPersists = credentialStore.GetCredential(myUri).Persist;
                    TranslationProviderCredential cred = new TranslationProviderCredential("", credPersists); //will this work??

                    cred = credentialStore.GetCredential(myUri);                                              //if credential is there then just get it
                    //add gt key to options
                    loadOptions.apiKey = cred.Credential;
                }
                else
                {
                    throw new TranslationProviderAuthenticationException();
                    //throwing this exception ends up causing Studio to call MtTranslationProviderWinFormsUI.GetCredentialsFromUser();
                    //which we use to prompt the user to enter credentials
                }
            }

            //construct new provider with options..these options are going to include the cred.credential and the cred.persists
            MtTranslationProvider tp = new MtTranslationProvider(loadOptions);

            return(tp);
        }
        public ITranslationProvider CreateTranslationProvider(Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore)
        {
            #region "CheckHandlesUri"
            if (!SupportsTranslationProviderUri(translationProviderUri))
            {
                throw new Exception(PluginResources.UriNotSupportedMessage);
            }
            #endregion

            //create options class based on URI passed to the method
            MtTranslationOptions loadOptions = new MtTranslationOptions(translationProviderUri);

            //start with MT...check if we are using MT
            if (loadOptions.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
            {
                Uri myUri = new Uri("mtenhancedprovidermst:///");
                if (credentialStore.GetCredential(myUri) != null)
                {
                    bool credPersists = credentialStore.GetCredential(myUri).Persist;
                    TranslationProviderCredential cred = new TranslationProviderCredential("", credPersists); //will this work??

                    cred = credentialStore.GetCredential(myUri); //if credential is there then just get it

                    GenericCredentials cred2 = new GenericCredentials(cred.Credential);//convert to generic credentials
                    //add creds to options
                    loadOptions.ClientID = cred2.UserName;
                    loadOptions.ClientSecret = cred2.Password;
                }
                else
                {
                    throw new TranslationProviderAuthenticationException();
                }
            }
            else //if we are using Google as the provider need to get API key
            {
                Uri myUri = new Uri("mtenhancedprovidergt:///");
                if (credentialStore.GetCredential(myUri) != null)
                {
                    bool credPersists = credentialStore.GetCredential(myUri).Persist;
                    TranslationProviderCredential cred = new TranslationProviderCredential("", credPersists); //will this work??

                    cred = credentialStore.GetCredential(myUri); //if credential is there then just get it
                    //add gt key to options
                    loadOptions.apiKey = cred.Credential;
                }
                else
                {
                    throw new TranslationProviderAuthenticationException();
                    //throwing this exception ends up causing Studio to call MtTranslationProviderWinFormsUI.GetCredentialsFromUser();
                    //which we use to prompt the user to enter credentials
                }
            }

            //construct new provider with options..these options are going to include the cred.credential and the cred.persists
            MtTranslationProvider tp = new MtTranslationProvider(loadOptions);

            return tp;
        }
Example #7
0
 /// <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 MtTranslationProviderLanguageDirection(MtTranslationProvider provider, LanguagePair languages)
 {
     #region "Instantiate"
     _provider          = provider;
     _languageDirection = languages;
     _options           = _provider.Options;
     _visitor           = new MtTranslationProviderElementVisitor(_options);
     #endregion
 }
 public MtTranslationProviderLanguageDirection(MtTranslationProvider provider, LanguagePair languages)
 {
     #region "Instantiate"
     _provider = provider;
     _languageDirection = languages;
     _options = _provider.Options;
     _visitor = new MtTranslationProviderElementVisitor(_options);
     #endregion
 }
        const string gTranslateString   = "Google Translate";     //these strings should not be localized or changed and are therefore hard-coded as constants


        #region "ProviderConfDialog"
        public MtProviderConfDialog(MtTranslationOptions options, ITranslationProviderCredentialStore credentialStore)
        {
            this.credstore = credentialStore;
            uriMs          = new Uri("mtenhancedprovidermst:///");
            uriGt          = new Uri("mtenhancedprovidergt:///");
            Options        = options;
            InitializeComponent();
            UpdateDialog();
        }
 public MtProviderConfDialog(MtTranslationOptions options, ITranslationProviderCredentialStore credentialStore)
 {
     this.credstore = credentialStore;
     uriMs = new Uri("mtenhancedprovidermst:///");
     uriGt = new Uri("mtenhancedprovidergt:///");
     Options = options;
     InitializeComponent();
     UpdateDialog();
 }
        public ITranslationProvider[] Browse(IWin32Window owner, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore)
        {
            //construct options to send to form
            MtTranslationOptions loadOptions = new MtTranslationOptions();
            //get saved key if there is one and put it into options
            //get google credentials
            TranslationProviderCredential getCredGt = GetMyCredentials(credentialStore, "mtenhancedprovidergt:///");

            if (getCredGt != null)
            {
                loadOptions.apiKey           = getCredGt.Credential;
                loadOptions.persistGoogleKey = getCredGt.Persist;
            }

            //get microsoft credentials
            TranslationProviderCredential getCredMt = GetMyCredentials(credentialStore, "mtenhancedprovidermst:///");

            if (getCredMt != null)
            {
                try
                {
                    GenericCredentials creds = new GenericCredentials(getCredMt.Credential); //parse credential into username and password
                    loadOptions.ClientID              = creds.UserName;
                    loadOptions.ClientSecret          = creds.Password;
                    loadOptions.persistMicrosoftCreds = getCredMt.Persist;
                }
                catch { } //swallow b/c it will just fail to fill in instead of crashing the whole program
            }

            //construct form
            MtProviderConfDialog dialog = new MtProviderConfDialog(loadOptions, credentialStore);

            //we are letting user delete creds but after testing it seems that it's ok if the individual credentials are null, b/c our method will re-add them to the credstore based on the uri
            if (dialog.ShowDialog(owner) == DialogResult.OK)
            {
                MtTranslationProvider testProvider = new MtTranslationProvider(dialog.Options);
                string apiKey = dialog.Options.apiKey;

                //we are setting credentials selectively based on the chosen provider to avoid saving the other if it is blank
                if (dialog.Options.SelectedProvider == MtTranslationOptions.ProviderType.GoogleTranslate)
                {
                    //set google credential
                    SetGoogleCredentials(credentialStore, apiKey, dialog.Options.persistGoogleKey);
                }
                else if (dialog.Options.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
                {
                    //set mst cred
                    GenericCredentials creds2 = new GenericCredentials(dialog.Options.ClientID, dialog.Options.ClientSecret);
                    SetMstCredentials(credentialStore, creds2, dialog.Options.persistMicrosoftCreds);
                }

                return(new ITranslationProvider[] { testProvider });
            }
            return(null);
        }
        public ITranslationProvider[] Browse(IWin32Window owner, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore)
        {
            //construct options to send to form
            MtTranslationOptions loadOptions = new MtTranslationOptions();
            //get saved key if there is one and put it into options
            //get google credentials
            TranslationProviderCredential getCredGt = GetMyCredentials(credentialStore, "mtenhancedprovidergt:///");
            if (getCredGt != null)
            {
                loadOptions.apiKey = getCredGt.Credential;
                loadOptions.persistGoogleKey = getCredGt.Persist;
            }

            //get microsoft credentials
            TranslationProviderCredential getCredMt = GetMyCredentials(credentialStore, "mtenhancedprovidermst:///");
            if (getCredMt != null)
            {
                try
                {
                    GenericCredentials creds = new GenericCredentials(getCredMt.Credential); //parse credential into username and password
                    loadOptions.ClientID = creds.UserName;
                    loadOptions.ClientSecret = creds.Password;
                    loadOptions.persistMicrosoftCreds = getCredMt.Persist;
                }
                catch { } //swallow b/c it will just fail to fill in instead of crashing the whole program
            }

            //construct form
            MtProviderConfDialog dialog = new MtProviderConfDialog(loadOptions, credentialStore);
            //we are letting user delete creds but after testing it seems that it's ok if the individual credentials are null, b/c our method will re-add them to the credstore based on the uri
            if (dialog.ShowDialog(owner) == DialogResult.OK)
            {
                MtTranslationProvider testProvider = new MtTranslationProvider(dialog.Options);
                string apiKey = dialog.Options.apiKey;

                //we are setting credentials selectively based on the chosen provider to avoid saving the other if it is blank
                if (dialog.Options.SelectedProvider == MtTranslationOptions.ProviderType.GoogleTranslate)
                {
                    //set google credential
                    SetGoogleCredentials(credentialStore, apiKey, dialog.Options.persistGoogleKey);
                }
                else if (dialog.Options.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
                {
                    //set mst cred
                    GenericCredentials creds2 = new GenericCredentials(dialog.Options.ClientID, dialog.Options.ClientSecret);
                    SetMstCredentials(credentialStore, creds2, dialog.Options.persistMicrosoftCreds);
                }

                return new ITranslationProvider[] { testProvider };
            }
            return null;
        }
Example #13
0
        private string LookupGT(string sourcetext, MtTranslationOptions options, string format)
        {
            //instantiate GtApiConnecter if necessary
            if (gtConnect == null)
            {
                // need to get and insert key
                gtConnect = new MtTranslationProviderGTApiConnecter(options.apiKey); //needs key
            }
            else
            {
                gtConnect.ApiKey = options.apiKey; //reset key in case it has been changed in dialog since GtApiConnecter was instantiated
            }

            string translatedText = gtConnect.Translate(_languageDirection, sourcetext, format);

            return(translatedText);
        }
        /// <summary>
        /// This gets called when a TranslationProviderAuthenticationException is thrown
        /// Since SDL Studio doesn't pass the provider instance here and even if we do a workaround...
        /// any new options set in the form that comes up are never saved to the project XML...
        /// so there is no way to change any options, only to provide the credentials
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="translationProviderUri"></param>
        /// <param name="translationProviderState"></param>
        /// <param name="credentialStore"></param>
        /// <returns></returns>
        #region "GetCredentialsFromUser"
        public bool GetCredentialsFromUser(IWin32Window owner, Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore)
        {
            MtTranslationOptions options = new MtTranslationOptions(translationProviderUri);
            string caption = "Credentials"; //default in case any problem retrieving localized resource below

            if (options.SelectedProvider == MtTranslationOptions.ProviderType.GoogleTranslate)
            {
                caption = PluginResources.PromptForCredentialsCaption_Google;
            }
            else if (options.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
            {
                caption = PluginResources.PromptForCredentialsCaption_Microsoft;
            }

            MtProviderConfDialog dialog = new MtProviderConfDialog(options, caption, credentialStore);

            dialog.DisableForCredentialsOnly(); //only show controls for setting credentials, as that is the only thing that will end up getting saved

            if (dialog.ShowDialog(owner) == DialogResult.OK)
            {
                string apiKey = dialog.Options.apiKey;

                if (options.SelectedProvider == MtTranslationOptions.ProviderType.GoogleTranslate)
                {
                    //set google credential
                    SetGoogleCredentials(credentialStore, apiKey, dialog.Options.persistGoogleKey);
                }
                else if (options.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
                {
                    //set mst cred
                    GenericCredentials creds2 = new GenericCredentials(dialog.Options.ClientID, dialog.Options.ClientSecret);
                    SetMstCredentials(credentialStore, creds2, dialog.Options.persistMicrosoftCreds);
                }
                return(true);
            }
            return(false);
        }
 public MtTranslationProvider(MtTranslationOptions options)
 {
     Options = options;
 }
        private string LookupMST(string sourcetext, MtTranslationOptions options, string format)
        {
            string catId = "";
            if (options.UseCatID)
                catId = _options.CatID;//only use specific category ID if the option is selected
            string sourcelang = _languageDirection.SourceCulture.ToString();
            string targetlang = _languageDirection.TargetCulture.ToString();

            //instantiate ApiConnecter if necessary
            if (mstConnect == null)
            {
                mstConnect = new MstTranslateConnect.ApiConnecter(_options);
            }
            else
            {
                mstConnect.resetCrd(options.ClientID, options.ClientSecret); //reset key in case it has been changed in dialog since GtApiConnecter was instantiated
            }

            string translatedText = mstConnect.Translate(sourcelang, targetlang, sourcetext, catId, format);
            return translatedText;
        }
        private string LookupGT(string sourcetext, MtTranslationOptions options, string format)
        {
            //instantiate GtApiConnecter if necessary
            if (gtConnect == null)
            {
                // need to get and insert key
                gtConnect = new MtTranslationProviderGTApiConnecter(options.apiKey); //needs key
            }
            else
            {
                gtConnect.ApiKey = options.apiKey; //reset key in case it has been changed in dialog since GtApiConnecter was instantiated
            }

            string translatedText = gtConnect.Translate(_languageDirection, sourcetext, format);

            return translatedText;
        }
        public TranslationProviderDisplayInfo GetDisplayInfo(Uri translationProviderUri, string translationProviderState)
        {
            TranslationProviderDisplayInfo info = new TranslationProviderDisplayInfo();
            MtTranslationOptions options = new MtTranslationOptions(translationProviderUri);
            info.TranslationProviderIcon = PluginResources.my_icon;

            if (options.SelectedProvider == MtTranslationOptions.ProviderType.GoogleTranslate)
            {
                info.Name = PluginResources.Google_NiceName;
                info.TooltipText = PluginResources.Google_Tooltip;
                info.SearchResultImage = PluginResources.my_image;
            }
            else if (options.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
            {
                info.Name = PluginResources.Microsoft_NiceName;
                info.TooltipText = PluginResources.Microsoft_Tooltip;
                info.SearchResultImage = PluginResources.microsoft_image;
            }
            else
            {
                info.Name = PluginResources.Plugin_NiceName;
                info.TooltipText = PluginResources.Plugin_Tooltip;
            }
            return info;
        }
        public bool GetCredentialsFromUser(IWin32Window owner, Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore)
        {
            MtTranslationOptions options = new MtTranslationOptions(translationProviderUri);
            string caption = "Credentials"; //default in case any problem retrieving localized resource below
            if (options.SelectedProvider == MtTranslationOptions.ProviderType.GoogleTranslate)
                caption = PluginResources.PromptForCredentialsCaption_Google;
            else if (options.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
                caption = PluginResources.PromptForCredentialsCaption_Microsoft;

            MtProviderConfDialog dialog = new MtProviderConfDialog(options, caption, credentialStore);
            dialog.DisableForCredentialsOnly(); //only show controls for setting credentials, as that is the only thing that will end up getting saved

            if (dialog.ShowDialog(owner) == DialogResult.OK)
            {
                string apiKey = dialog.Options.apiKey;

                if (options.SelectedProvider == MtTranslationOptions.ProviderType.GoogleTranslate)
                {
                    //set google credential
                    SetGoogleCredentials(credentialStore, apiKey, dialog.Options.persistGoogleKey);
                }
                else if (options.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator)
                {
                    //set mst cred
                    GenericCredentials creds2 = new GenericCredentials(dialog.Options.ClientID, dialog.Options.ClientSecret);
                    SetMstCredentials(credentialStore, creds2, dialog.Options.persistMicrosoftCreds);
                }
                return true;
            }
            return false;
        }
Example #20
0
 public MtTranslationProviderElementVisitor(MtTranslationOptions options)
 {
     _options = options;
 }
 public MtTranslationProvider(MtTranslationOptions options)
 {
     Options = options;
 }