private void btn_OK_Click(object sender, EventArgs e) { if (!ValidateForm()) { return; } Options.ApiKey = textApiKey.Text; Options.ClientId = txtClientId.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; Options.LanguagesSupported = _correspondingLanguages?.ToDictionary(lp => lp.TargetCultureName, lp => Options.SelectedProvider.ToString()); this.DialogResult = DialogResult.OK; this.Close(); //dispose???? }
//TODO PACH (06/04/2021): Confirm if this is still required/ remove if obsolete code /// <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> public bool GetCredentialsFromUser(IWin32Window owner, Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore) { var projectController = SdlTradosStudio.Application.GetController <ProjectsController>(); var projectInfo = projectController?.CurrentProject?.GetProjectInfo(); var languagePairs = new List <LanguagePair>(); if (projectInfo != null) { foreach (var targetLanguage in projectInfo.TargetLanguages) { var languagePair = new LanguagePair(projectInfo.SourceLanguage.CultureInfo, targetLanguage.CultureInfo); languagePairs.Add(languagePair); } } var options = new MtTranslationOptions(); var regionsProvider = new RegionsProvider(); var mainWindowVm = ShowProviderWindow(languagePairs.ToArray(), credentialStore, options, regionsProvider); if (!mainWindowVm.DialogResult) { return(false); } return(mainWindowVm.DialogResult); }
/// <summary> /// Used for displaying the plug-in info such as the plug-in name, /// tooltip, and icon. /// </summary> public TranslationProviderDisplayInfo GetDisplayInfo(Uri translationProviderUri, string translationProviderState) { var info = new TranslationProviderDisplayInfo(); var options = new MtTranslationOptions(translationProviderUri); info.TranslationProviderIcon = PluginResources.my_icon; if (options.SelectedProvider == MtTranslationOptions.ProviderType.GoogleTranslate) { if (options.SelectedGoogleVersion == Enums.GoogleApiVersion.V2) { info.Name = PluginResources.GoogleBasic; info.TooltipText = PluginResources.GoogleBasic; } else { info.Name = PluginResources.GoogleAdvanced; info.TooltipText = PluginResources.GoogleAdvanced; } 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); }
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; 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 { } }
public ITranslationProvider[] Browse(IWin32Window owner, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore) { //construct options to send to form var loadOptions = new MtTranslationOptions(); //get saved key if there is one and put it into options //get google credentials var getCredGt = GetMyCredentials(credentialStore, "mtenhancedprovidergt:///"); if (getCredGt != null) { loadOptions.ApiKey = getCredGt.Credential; loadOptions.PersistGoogleKey = getCredGt.Persist; } //get microsoft credentials var getCredMt = GetMyCredentials(credentialStore, "mtenhancedprovidermst:///"); if (getCredMt != null) { try { var 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 } var apiConnecter = new ApiConnecter(loadOptions); var allSupportedLanguages = ApiConnecter.SupportedLangs; var correspondingLanguages = languagePairs.Where(lp => allSupportedLanguages.Contains(lp.TargetCultureName.Substring(0, 2))).ToList(); //loadOptions.LanguagesSupported = correspLanguages.ToDictionary(lp => lp.TargetCultureName, lp=>"MS Translator"); //construct form var dialog = new MtProviderConfDialog(loadOptions, credentialStore, correspondingLanguages); //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) { var testProvider = new MtTranslationProvider(dialog.Options); var 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 var creds2 = new GenericCredentials(dialog.Options.ClientId, dialog.Options.ClientSecret); SetMstCredentials(credentialStore, creds2, dialog.Options.PersistMicrosoftCreds); } return(new ITranslationProvider[] { testProvider }); } return(null); }
private string LookupMst(string sourcetext, MtTranslationOptions options, string format) { var catId = ""; if (options.UseCatID) { catId = _options.CatId; //only use specific category ID if the option is selected } var sourcelang = _languageDirection.SourceCulture.ToString(); var targetlang = _languageDirection.TargetCulture.ToString(); //instantiate ApiConnecter if necessary if (_mstConnect == null) { _mstConnect = new ApiConnecter(_options); } else { _mstConnect.resetCrd(options.ClientId, options.ClientSecret); //reset key in case it has been changed in dialog since GtApiConnecter was instantiated } var translatedText = _mstConnect.Translate(sourcelang, targetlang, sourcetext, catId, format); return(translatedText); }
/// <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; #endregion }
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 var loadOptions = new MtTranslationOptions(translationProviderUri); //start with MT...check if we are using MT if (loadOptions.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator) { var myUri = new Uri("mtenhancedprovidermst:///"); if (credentialStore.GetCredential(myUri) != null) { var credPersists = credentialStore.GetCredential(myUri).Persist; var cred = new TranslationProviderCredential("", credPersists); //will this work?? cred = credentialStore.GetCredential(myUri); //if credential is there then just get it var 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 { var myUri = new Uri("mtenhancedprovidergt:///"); if (credentialStore.GetCredential(myUri) != null) { var credPersists = credentialStore.GetCredential(myUri).Persist; var 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 var tp = new MtTranslationProvider(loadOptions); return tp; }
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(); }
/// <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 MtProviderConfDialog(MtTranslationOptions options, bool isTellMeAction) { _isTellMeAction = isTellMeAction; uriMs = new Uri("mtenhancedprovidermst:///"); uriGt = new Uri("mtenhancedprovidergt:///"); Options = options; InitializeComponent(); UpdateDialog(); tabControl1.SelectedTab = tabPage3; tabPage1.Enabled = false; }
public MtProviderConfDialog(MtTranslationOptions options, ITranslationProviderCredentialStore credentialStore, List <LanguagePair> correspondingLanguages) { this.credstore = credentialStore; uriMs = new Uri("mtenhancedprovidermst:///"); uriGt = new Uri("mtenhancedprovidergt:///"); Options = options; InitializeComponent(); UpdateDialog(); if (correspondingLanguages != null) { _correspondingLanguages = correspondingLanguages; } }
public ITranslationProvider CreateTranslationProvider(Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore) { if (!SupportsTranslationProviderUri(translationProviderUri)) { throw new Exception(PluginResources.UriNotSupportedMessage); } //create options class based on URI passed to the method var loadOptions = new MtTranslationOptions(translationProviderUri); var regionsProvider = new RegionsProvider(); //start with MT...check if we are using MT if (loadOptions.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator) { var myUri = new Uri(PluginResources.UriMs); if (credentialStore.GetCredential(myUri) != null) { var cred = new TranslationProviderCredential(credentialStore.GetCredential(myUri).Credential, credentialStore.GetCredential(myUri).Persist); loadOptions.ClientId = cred.Credential; loadOptions.PersistMicrosoftCreds = cred.Persist; } else { throw new TranslationProviderAuthenticationException(); } } else //if we are using Google as the provider need to get API key { var myUri = new Uri(PluginResources.UriGt); if (credentialStore.GetCredential(myUri) != null) { var cred = new TranslationProviderCredential(credentialStore.GetCredential(myUri).Credential, credentialStore.GetCredential(myUri).Persist); loadOptions.ApiKey = cred.Credential; loadOptions.PersistGoogleKey = cred.Persist; } 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 var tp = new MtTranslationProvider(loadOptions, regionsProvider); return(tp); }
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 } var translatedText = _gtConnect.Translate(_languageDirection, sourcetext, format); return(translatedText); }
public ITranslationProvider[] Browse(IWin32Window owner, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore) { var options = new MtTranslationOptions(); var regionsProvider = new RegionsProvider(); var mainWindowVm = ShowProviderWindow(languagePairs, credentialStore, options, regionsProvider); if (!mainWindowVm.DialogResult) { return(null); } var provider = new MtTranslationProvider(options, regionsProvider); return(new ITranslationProvider[] { provider }); }
/// <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) { var options = new MtTranslationOptions(translationProviderUri); var 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; } var 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) { var 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 var creds2 = new GenericCredentials(dialog.Options.ClientId, dialog.Options.ClientSecret); SetMstCredentials(credentialStore, creds2, dialog.Options.PersistMicrosoftCreds); } return(true); } return(false); }
public MtTranslationProviderElementVisitor(MtTranslationOptions options) { _options = options; }
/// <summary> /// Instantiates the variables and fills the list file content into /// a Dictionary collection object. /// </summary> /// <param name="provider"></param> /// <param name="languages"></param> public MtTranslationProviderLanguageDirection(MtTranslationProvider provider, LanguagePair languages) { _provider = provider; _languageDirection = languages; _options = _provider.Options; }
public MtTranslationProvider(MtTranslationOptions options) { Options = options; }
public ITranslationProvider CreateTranslationProvider(Uri translationProviderUri, string translationProviderState, ITranslationProviderCredentialStore credentialStore) { if (!SupportsTranslationProviderUri(translationProviderUri)) { throw new Exception(PluginResources.UriNotSupportedMessage); } //create options class based on URI passed to the method var loadOptions = new MtTranslationOptions(translationProviderUri); var regionsProvider = new RegionsProvider(); var htmlUtil = new HtmlUtil(); //start with MT...check if we are using MT if (loadOptions.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator) { // The credential is saved with a different URI scheme than that of the plugin! // We will need to make this known and/or provide a workaround in identifying the credentials // added from the project automation API. // The following is a work-around which attempts to recover the credential, given various scenarios var credential = credentialStore.GetCredential(new Uri(PluginResources.UriMs)) ?? credentialStore.GetCredential(translationProviderUri) ?? credentialStore.GetCredential(new Uri(translationProviderUri.Scheme + ":///")); if (credential != null) { var cred = new TranslationProviderCredential(credential.Credential, credential.Persist); loadOptions.ClientId = cred.Credential; loadOptions.PersistMicrosoftCreds = cred.Persist; } else { throw new TranslationProviderAuthenticationException(); } } else //if we are using Google as the provider need to get API key { // The credential is saved with a different URI scheme than that of the plugin! // We will need to make this known and/or provide a workaround in identifying the credentials // added from the project automation API. // The following is a work-around which attempts to recover the credential, given various scenarios var credential = credentialStore.GetCredential(new Uri(PluginResources.UriGt)) ?? credentialStore.GetCredential(translationProviderUri) ?? credentialStore.GetCredential(new Uri(translationProviderUri.Scheme + ":///")); if (credential != null) { var cred = new TranslationProviderCredential(credential.Credential, credential.Persist); loadOptions.ApiKey = cred.Credential; loadOptions.PersistGoogleKey = cred.Persist; } 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 var tp = new MtTranslationProvider(loadOptions, regionsProvider, htmlUtil); return(tp); }