Пример #1
0
        public ITranslationProvider[] Browse(IWin32Window owner, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore)
        {
            try
            {
                var options     = new BeGlobalTranslationOptions();
                var token       = string.Empty;
                var credentials = GetCredentials(credentialStore, "sdlmachinetranslationcloudprovider:///");

                var beGlobalVm = new BeGlobalWindowViewModel(options, languagePairs, credentials);
                beGlobalVm.BeGlobalWindow.DataContext = beGlobalVm;

                beGlobalVm.BeGlobalWindow.ShowDialog();
                if (beGlobalVm.BeGlobalWindow.DialogResult.HasValue && beGlobalVm.BeGlobalWindow.DialogResult.Value)
                {
                    var messageBoxService = new MessageBoxService();
                    beGlobalVm.Options.ClientId     = beGlobalVm.BeGlobalWindow.ClientIdBox?.Password.TrimEnd();
                    beGlobalVm.Options.ClientSecret = beGlobalVm.BeGlobalWindow.ClientSecretBox?.Password.TrimEnd();
                    var beGlobalService = new BeGlobalV4Translator(beGlobalVm.Options, messageBoxService, credentials);
                    beGlobalVm.Options.BeGlobalService = beGlobalService;

                    var provider = new BeGlobalTranslationProvider(options)
                    {
                        Options = beGlobalVm.Options
                    };

                    SetCredentials(credentialStore, beGlobalVm.Options.ClientId, beGlobalVm.Options.ClientSecret, true);
                    return(new ITranslationProvider[] { provider });
                }
            }
            catch (Exception e)
            {
                Log.Logger.Error($"{Constants.Browse} {e.Message}\n {e.StackTrace}");
            }
            return(null);
        }
        public 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 BeGlobalTagPlacer(newseg);
                            sourceText = tagPlacer.PreparedSourceText;
                        }
                        else
                        {
                            sourceText = newseg.ToPlain();
                        }

                        sourceText = _normalizeSourceTextHelper.NormalizeText(sourceText);
                        preTranslatesegments[i].SourceText = sourceText;
                    }
                }

                var sourceLanguage =
                    _normalizeSourceTextHelper.GetCorespondingLangCode(_languageDirection.SourceCulture);
                var targetLanguage =
                    _normalizeSourceTextHelper.GetCorespondingLangCode(_languageDirection.TargetCulture);

                var translator = new BeGlobalV4Translator("https://translate-api.sdlbeglobal.com", _options.ClientId,
                                                          _options.ClientSecret, sourceLanguage, targetLanguage, _options.Model, _options.UseClientAuthentication);

                await Task.Run(() => Parallel.ForEach(preTranslatesegments, segment =>
                {
                    if (segment != null)
                    {
                        var translation          = HttpUtility.UrlDecode(translator.TranslateText(segment.SourceText));
                        segment.PlainTranslation = HttpUtility.HtmlDecode(translation);
                    }
                })).ConfigureAwait(true);

                return(preTranslatesegments);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(preTranslatesegments);
        }
Пример #3
0
        public string Translate(LanguagePair languageDirection, string sourceText)
        {
            var targetLanguage = _normalizeTextHelper.GetCorespondingLangCode(languageDirection.TargetCulture);
            var sourceLanguage = _normalizeTextHelper.GetCorespondingLangCode(languageDirection.SourceCulture);

            sourceText = _normalizeTextHelper.NormalizeText(sourceText);

            var beGlobalTranslator = new BeGlobalV4Translator("https://translate-api.sdlbeglobal.com", ClientId, ClientSecret,
                                                              sourceLanguage, targetLanguage, _model, UseClientAuthentication);

            var translatedText = HttpUtility.UrlDecode(beGlobalTranslator.TranslateText(sourceText));

            translatedText = HttpUtility.HtmlDecode(translatedText);

            return(translatedText);
        }
Пример #4
0
        public BeGlobalWindowViewModel(BeGlobalWindow mainWindow, BeGlobalTranslationOptions options, LanguagePair[] languagePairs)
        {
            Options                    = options;
            _mainWindow                = mainWindow;
            _languagePairs             = languagePairs;
            _normalizeSourceTextHelper = new NormalizeSourceTextHelper();
            TranslationOptions         = new ObservableCollection <TranslationModel>();

            var beGlobalTranslator = new BeGlobalV4Translator(Options?.Model);
            var accountId          = beGlobalTranslator.GetUserInformation();

            var subscriptionInfo = beGlobalTranslator.GetLanguagePairs(accountId.ToString());

            GetEngineModels(subscriptionInfo.LanguagePairs);
            SetEngineModel();
        }
Пример #5
0
        public ITranslationProvider[] Browse(IWin32Window owner, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore)
        {
            try
            {
                AppItializer.EnsureInitializer();
                var options = new BeGlobalTranslationOptions();
                var token   = string.Empty;

                Application.Current?.Dispatcher?.Invoke(() =>
                {
                    token = _studioCredentials.GetToken();
                });
                if (!string.IsNullOrEmpty(token))
                {
                    var beGlobalWindow = new BeGlobalWindow();
                    var beGlobalVm     = new BeGlobalWindowViewModel(beGlobalWindow, options, languagePairs);
                    beGlobalWindow.DataContext = beGlobalVm;

                    beGlobalWindow.ShowDialog();
                    if (beGlobalWindow.DialogResult.HasValue && beGlobalWindow.DialogResult.Value)
                    {
                        var beGlobalService = new BeGlobalV4Translator(beGlobalVm.Options.Model);
                        beGlobalVm.Options.BeGlobalService = beGlobalService;
                        var provider = new BeGlobalTranslationProvider(options)
                        {
                            Options = beGlobalVm.Options
                        };
                        return(new ITranslationProvider[] { provider });
                    }
                }
            }
            catch (Exception e)
            {
                Log.Logger.Error($"Browse: {e.Message}\n {e.StackTrace}");
            }
            return(null);
        }
Пример #6
0
        public BeGlobalWindowViewModel(BeGlobalTranslationOptions options, LanguagePair[] languagePairs, TranslationProviderCredential credentials)
        {
            SelectedTabIndex           = 0;
            Options                    = options;
            LoginViewModel             = new LoginViewModel(options);
            LanguageMappingsViewModel  = new LanguageMappingsViewModel(options);
            _languagePairs             = languagePairs;
            _normalizeSourceTextHelper = new NormalizeSourceTextHelper();
            _credentials               = credentials;

            TranslationOptions = new ObservableCollection <TranslationModel>();
            _messageBoxService = new MessageBoxService();
            if (Options != null)
            {
                ReSendChecked = options.ResendDrafts;
            }

            if (credentials == null)
            {
                return;
            }
            var credential = _credentials.Credential.Replace("sdlmachinetranslationcloudprovider:///", string.Empty);

            if (credential.Contains("#"))
            {
                var splitedCredentials = credentials.Credential.Split('#');
                if (splitedCredentials.Length == 2 && !string.IsNullOrEmpty(splitedCredentials[0]) && !string.IsNullOrEmpty(splitedCredentials[1]))
                {
                    var beGlobalTranslator = new BeGlobalV4Translator(Options, _messageBoxService, _credentials);
                    var accountId          = beGlobalTranslator.GetUserInformation();
                    var subscriptionInfo   = beGlobalTranslator.GetLanguagePairs(accountId.ToString());
                    GetEngineModels(subscriptionInfo);
                    SetEngineModel();
                    SetAuthenticationOptions();
                }
            }
        }
Пример #7
0
        public ITranslationProvider CreateTranslationProvider(Uri translationProviderUri, string translationProviderState,
                                                              ITranslationProviderCredentialStore credentialStore)
        {
            var originalUri = new Uri("beglobaltranslationprovider:///");
            var options     = new BeGlobalTranslationOptions(translationProviderUri);

            if (credentialStore.GetCredential(originalUri) != null)
            {
                var credentials        = credentialStore.GetCredential(originalUri);
                var splitedCredentials = credentials.Credential.Split('#');
                options.ClientId     = splitedCredentials[0];
                options.ClientSecret = splitedCredentials[1];
            }
            else
            {
                credentialStore.AddCredential(originalUri, new TranslationProviderCredential(originalUri.ToString(), true));
            }

            var beGlobalTranslator = new BeGlobalV4Translator("https://translate-api.sdlbeglobal.com", options.ClientId,
                                                              options.ClientSecret, string.Empty, string.Empty, options.Model, options.UseClientAuthentication);

            int accountId;

            if (options.UseClientAuthentication)
            {
                accountId = beGlobalTranslator.GetClientInformation();
            }
            else
            {
                accountId = beGlobalTranslator.GetUserInformation();
            }
            var subscriptionInfo = beGlobalTranslator.GetLanguagePairs(accountId.ToString());

            options.SubscriptionInfo = subscriptionInfo;
            return(new BeGlobalTranslationProvider(options));
        }