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> /// Determines the language direction of the delimited list file by /// reading the first line. Based upon this information it is determined /// whether the plug-in supports the language pair that was selected by /// the user. /// </summary> public bool SupportsLanguageDirection(LanguagePair languageDirection) { if (Options.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator) { if (mstConnect == null) //construct ApiConnecter if necessary { mstConnect = new ApiConnecter(Options); } else { mstConnect.resetCrd(Options.ClientId, Options.ClientSecret); //reset in case changed since last time the class was constructed } return(mstConnect.isSupportedLangPair(languageDirection.SourceCulture.Name, languageDirection.TargetCulture.Name)); } if (Options.SelectedProvider == MtTranslationOptions.ProviderType.GoogleTranslate) { if (gtConnect == null) //instantiate GtApiConnecter if necessary { gtConnect = new MtTranslationProviderGTApiConnecter(Options.ApiKey); } else { gtConnect.ApiKey = Options.ApiKey; //reset in case it has been changed since last time GtApiConnecter was instantiated } return(gtConnect.IsSupportedLangPair(languageDirection.SourceCulture, languageDirection.TargetCulture)); } //not likely to get here but... return(true); }
/// <summary> /// Determines the language direction of the delimited list file by /// reading the first line. Based upon this information it is determined /// whether the plug-in supports the language pair that was selected by /// the user. /// </summary> public bool SupportsLanguageDirection(LanguagePair languageDirection) { if (Options.SelectedProvider == MtTranslationOptions.ProviderType.MicrosoftTranslator) { if (_mstConnect == null) //construct ApiConnecter if necessary { _mstConnect = new ApiConnecter(Options.ClientId, Options.Region); } else { _mstConnect.ResetCrd(Options.ClientId, Options.Region); //reset in case changed since last time the class was constructed } return(_mstConnect.IsSupportedLangPair(languageDirection.SourceCulture.Name, languageDirection.TargetCulture.Name)); } if (Options.SelectedGoogleVersion == Enums.GoogleApiVersion.V2) { if (_gtConnect == null) //instantiate GtApiConnecter if necessary { _gtConnect = new MtTranslationProviderGTApiConnecter(Options.ApiKey); } else { _gtConnect.ApiKey = Options.ApiKey; //reset in case it has been changed since last time GtApiConnecter was instantiated } return(_gtConnect.IsSupportedLangPair(languageDirection.SourceCulture, languageDirection.TargetCulture)); } _googleV3Connecter = new GoogleV3Connecter(Options); return(_googleV3Connecter.IsSupportedLanguage(languageDirection.SourceCulture, languageDirection.TargetCulture)); }
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 static void SetSupportedLanguages(LanguagePair[] languagePairs, MtTranslationOptions loadOptions) { var apiConnecter = new ApiConnecter(loadOptions); foreach (var languagePair in languagePairs) { var supportedLanguages = apiConnecter.GetSupportedLanguages(); var targetLanguage = languagePair.TargetCultureName.Substring(0, 2).ToLower(); if (supportedLanguages.Contains(targetLanguage) && !loadOptions.LanguagesSupported.ContainsKey(targetLanguage)) { loadOptions.LanguagesSupported.Add(languagePair.TargetCultureName, "Amazon Translate"); } } }
/// <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, in our implementation /// the delimiter character and the list file name. /// </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) { var editProvider = translationProvider as MtTranslationProvider; if (editProvider == null) { return(false); } //get saved key if there is one and put it into options //get google credentials var getCredGt = GetMyCredentials(credentialStore, "mtenhancedprovidergt:///"); if (getCredGt != null) { editProvider.Options.ApiKey = getCredGt.Credential; editProvider.Options.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 editProvider.Options.ClientId = creds.UserName; editProvider.Options.ClientSecret = creds.Password; editProvider.Options.PersistMicrosoftCreds = getCredMt.Persist; } catch (Exception ex) //swallow b/c it will just fail to fill in instead of crashing the whole program { Log.Logger.Error($"{_constants.Edit} {ex.Message}\n { ex.StackTrace}"); } } var apiConnecter = new ApiConnecter(editProvider.Options); var allSupportedLanguages = ApiConnecter.SupportedLangs; var correspondingLanguages = languagePairs.Where(lp => allSupportedLanguages.Contains(lp.TargetCultureName.Substring(0, 2))).ToList(); var dialog = new MtProviderConfDialog(editProvider.Options, 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) { editProvider.Options = dialog.Options; var apiKey = editProvider.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 credentials = new GenericCredentials(dialog.Options.ClientId, dialog.Options.ClientSecret); SetMstCredentials(credentialStore, credentials, dialog.Options.PersistMicrosoftCreds); } return(true); } return(false); }