コード例 #1
0
        private LanguageMappingModel GetLanguageMappingModel(InternalLanguageMapping mapping)
        {
            // assign the selected model
            var selectedModel = mapping.EngineModels.FirstOrDefault(a => a.DisplayName.Equals(mapping.SavedLanguageMappingModel?.SelectedModel?.DisplayName, StringComparison.InvariantCultureIgnoreCase))
                                ?? mapping.EngineModels.FirstOrDefault(a => a.Model.Equals("generic", StringComparison.InvariantCultureIgnoreCase))
                                ?? mapping.EngineModels[0];

            var dictionaries = LanguageMappingsService.GetDictionaries(mapping.SelectedSourceLanguageMapping, mapping.SelectedTargetLanguageMapping);

            // assign the selected dictionary
            var selectedDictionary =
                dictionaries.FirstOrDefault(a => a.Name.Equals(mapping.SavedLanguageMappingModel?.SelectedDictionary?.Name))
                ?? dictionaries[0];

            var languageMappingModel = new LanguageMappingModel
            {
                Name               = mapping.Name,
                SourceLanguages    = mapping.SourceLanguageMappings,
                TargetLanguages    = mapping.TargetLanguageMappings,
                SelectedSource     = mapping.SelectedSourceLanguageMapping,
                SelectedTarget     = mapping.SelectedTargetLanguageMapping,
                SourceTradosCode   = mapping.SourceLanguageCode.TradosCode,
                TargetTradosCode   = mapping.TargetLanguageCode.TradosCode,
                Models             = mapping.EngineModels,
                SelectedModel      = selectedModel,
                Dictionaries       = dictionaries,
                SelectedDictionary = selectedDictionary
            };

            return(languageMappingModel);
        }
コード例 #2
0
        public void UpdateLanguageMappingModel(LanguageMappingModel languageMappingModel)
        {
            var translationModels = LanguageMappingsService.GetTranslationModels(
                languageMappingModel.SelectedSource, languageMappingModel.SelectedTarget, languageMappingModel.SourceTradosCode,
                languageMappingModel.TargetTradosCode);

            if (translationModels.Any())
            {
                // assign the selected model
                var selectedModel =
                    translationModels.FirstOrDefault(a => string.Compare(a.DisplayName,
                                                                         languageMappingModel.SelectedModel?.DisplayName,
                                                                         StringComparison.InvariantCultureIgnoreCase) == 0)
                    ?? translationModels.FirstOrDefault(a =>
                                                        string.Compare(a.Model, "generic", StringComparison.InvariantCultureIgnoreCase) == 0)
                    ?? translationModels[0];

                languageMappingModel.Models        = translationModels;
                languageMappingModel.SelectedModel = selectedModel;
            }

            var dictionaries =
                LanguageMappingsService.GetDictionaries(languageMappingModel.SelectedSource,
                                                        languageMappingModel.SelectedTarget);

            // assign the selected dictionary
            var selectedDictionary =
                dictionaries.FirstOrDefault(a => a.Name.Equals(languageMappingModel.SelectedDictionary?.Name))
                ?? dictionaries[0];

            languageMappingModel.Dictionaries       = dictionaries;
            languageMappingModel.SelectedDictionary = selectedDictionary;
        }
コード例 #3
0
 public SdlMTCloudTranslationProvider(SdlMTCloudTranslationOptions options)
 {
     Options = options;
     _languageMappingsService = new LanguageMappingsService();
     _encryptedClientId       = StringExtensions.EncryptData(Options.ClientId);
     _encryptedClientSecret   = StringExtensions.EncryptData(Options.ClientSecret);
 }
コード例 #4
0
        public ITranslationProvider[] Browse(IWin32Window owner, LanguagePair[] languagePairs, ITranslationProviderCredentialStore credentialStore)
        {
            try
            {
                var uri = new Uri($"{Constants.MTCloudUriScheme}://");
                var connectionService = new ConnectionService(owner, new VersionService(), LanguageCloudIdentityApi.Instance);

                var credential       = connectionService.GetCredential(credentialStore);
                var connectionResult = connectionService.EnsureSignedIn(credential, true);

                if (!connectionResult.Item1)
                {
                    throw new TranslationProviderAuthenticationException(PluginResources.Message_Invalid_credentials);
                }

                connectionService.SaveCredential(credentialStore);

                var languageMappingsService = new LanguageMappingsService();
                var translationService      = new TranslationService(connectionService, languageMappingsService);
                var editorController        = SdlTradosStudio.Application?.GetController <EditorController>();

                var provider = new SdlMTCloudTranslationProvider(uri, translationService, string.Empty, editorController);

                var optionsViewModel = new OptionsViewModel(null, provider, languagePairs);
                optionsViewModel.SaveLanguageMappings();

                return(new ITranslationProvider[] { provider });
            }
            catch (Exception e)
            {
                Log.Logger.Error($"{Constants.Browse} {e.Message}\n {e.StackTrace}");
            }

            return(null);
        }
コード例 #5
0
        private void ValidateEngineExistence(InternalLanguageMapping mapping)
        {
            // attempt to recover the language model from the secondary target language code
            if (NoEngineFound(mapping.EngineModels) && mapping.TargetLanguageMappings.Count > 1)
            {
                var secondaryLanguageCode = mapping.TargetLanguageMappings.FirstOrDefault(a => a.CodeName != mapping.SelectedTargetLanguageMapping.CodeName);

                var secondaryEngineModels = LanguageMappingsService.GetTranslationModels(mapping.SelectedSourceLanguageMapping, secondaryLanguageCode,
                                                                                         mapping.SourceLanguageCode.TradosCode, mapping.TargetLanguageCode.TradosCode);

                if (secondaryEngineModels.Any())
                {
                    mapping.EngineModels = secondaryEngineModels;
                    mapping.SelectedTargetLanguageMapping = secondaryLanguageCode;
                }
            }

            // attempt to recover the language model from the secondary source language code
            if (NoEngineFound(mapping.EngineModels) && mapping.SourceLanguageMappings.Count > 1)
            {
                var secondarySourceLanguageCode = mapping.SourceLanguageMappings.FirstOrDefault(a => a.CodeName != mapping.SelectedSourceLanguageMapping.CodeName);

                var secondaryEngineModels = LanguageMappingsService.GetTranslationModels(secondarySourceLanguageCode, mapping.SelectedTargetLanguageMapping,
                                                                                         mapping.SourceLanguageCode.TradosCode, mapping.TargetLanguageCode.TradosCode);

                if (secondaryEngineModels.Any())
                {
                    mapping.EngineModels = secondaryEngineModels;
                    mapping.SelectedSourceLanguageMapping = secondarySourceLanguageCode;
                }
                else if (mapping.TargetLanguageMappings.Count > 1)
                {
                    // attempt to recover the language model from the secondary source OR target language code
                    var secondaryTargetLanguageCode = mapping.TargetLanguageMappings.FirstOrDefault(a => a.CodeName != mapping.SelectedTargetLanguageMapping?.CodeName);

                    secondaryEngineModels = LanguageMappingsService.GetTranslationModels(
                        secondarySourceLanguageCode, secondaryTargetLanguageCode, mapping.SourceLanguageCode.TradosCode,
                        mapping.TargetLanguageCode.TradosCode);

                    if (secondaryEngineModels.Any())
                    {
                        mapping.EngineModels = secondaryEngineModels;
                        mapping.SelectedSourceLanguageMapping = secondarySourceLanguageCode;
                        mapping.SelectedTargetLanguageMapping = secondaryTargetLanguageCode;
                    }
                }
            }
        }
コード例 #6
0
        public LanguageMappingModel GetLanguageMappingModel(LanguagePair languageDirection, List <MappedLanguage> mappedLanguages)
        {
            var mapping = new InternalLanguageMapping
            {
                SourceLanguageCode = mappedLanguages?.FirstOrDefault(s => s.TradosCode.Equals(languageDirection.SourceCulture?.Name))
            };

            if (mapping.SourceLanguageCode == null)
            {
                return(null);
            }

            mapping.SourceLanguageMappings        = LanguageMappingsService.GetMTCloudLanguages(mapping.SourceLanguageCode, languageDirection.SourceCulture);
            mapping.SelectedSourceLanguageMapping = mapping.SourceLanguageMappings.FirstOrDefault(a => a.IsLocale) ?? mapping.SourceLanguageMappings[0];

            mapping.TargetLanguageCode = mappedLanguages.FirstOrDefault(s => s.TradosCode.Equals(languageDirection.TargetCulture?.Name));
            if (mapping.TargetLanguageCode == null)
            {
                return(null);
            }

            mapping.Name = $"{languageDirection.SourceCulture?.DisplayName} - {languageDirection.TargetCulture?.DisplayName}";
            mapping.SavedLanguageMappingModel = Options.LanguageMappings.FirstOrDefault(a => a.Name.Equals(mapping.Name, StringComparison.InvariantCultureIgnoreCase));

            mapping.TargetLanguageMappings        = LanguageMappingsService.GetMTCloudLanguages(mapping.TargetLanguageCode, languageDirection.TargetCulture);
            mapping.SelectedTargetLanguageMapping = mapping.TargetLanguageMappings.FirstOrDefault(a => a.IsLocale) ?? mapping.TargetLanguageMappings[0];

            // assign the selected target langauge
            mapping.SelectedTargetLanguageMapping = mapping.TargetLanguageMappings.FirstOrDefault(a => a.CodeName.Equals(mapping.SavedLanguageMappingModel?.SelectedTarget?.CodeName))
                                                    ?? mapping.SelectedTargetLanguageMapping;

            mapping.EngineModels = LanguageMappingsService.GetTranslationModels(mapping.SelectedSourceLanguageMapping, mapping.SelectedTargetLanguageMapping,
                                                                                mapping.SourceLanguageCode.TradosCode, mapping.TargetLanguageCode.TradosCode);

            if (mapping.SavedLanguageMappingModel?.SelectedModel.DisplayName != PluginResources.Message_No_model_available)
            {
                ValidateEngineExistence(mapping);
            }

            if (mapping.EngineModels.Any())
            {
                var languageMappingModel = GetLanguageMappingModel(mapping);
                return(languageMappingModel);
            }

            return(null);
        }
コード例 #7
0
        public LanguageMappingsViewModel(SdlMTCloudTranslationOptions options, OptionsWindowModel beGlobalWindowViewModel,
                                         LanguagePair[] languagePairs, Languages.Provider.Languages languages)
        {
            _mtCloudDictionaries = new List <MTCloudDictionary>();
            _languages           = languages;
            _languagePairs       = languagePairs;

            Options                  = options;
            _languageMappings        = new ObservableCollection <LanguageMappingModel>();
            _beGlobalWindowViewModel = beGlobalWindowViewModel;

            _languageMappingService = new LanguageMappingsService();

            if (Options != null)
            {
                ReSendChecked = options.ResendDrafts;
            }

            BindingOperations.EnableCollectionSynchronization(LanguageMappings, _languageMappings);
        }
コード例 #8
0
        public ITranslationProvider CreateTranslationProvider(Uri translationProviderUri, string translationProviderState,
                                                              ITranslationProviderCredentialStore credentialStore)
        {
            var connectionService = new ConnectionService(StudioInstance.GetActiveForm(), new VersionService(), LanguageCloudIdentityApi.Instance);

            var credential       = connectionService.GetCredential(credentialStore);
            var connectionResult = connectionService.EnsureSignedIn(credential);

            if (!connectionResult.Item1)
            {
                throw new TranslationProviderAuthenticationException(PluginResources.Message_Invalid_credentials);
            }

            connectionService.SaveCredential(credentialStore);

            var languageMappingsService = new LanguageMappingsService();
            var translationService      = new TranslationService(connectionService, languageMappingsService);
            var editorController        = SdlTradosStudio.Application?.GetController <EditorController>();

            var provider = new SdlMTCloudTranslationProvider(translationProviderUri, translationService, translationProviderState, editorController);

            return(provider);
        }
コード例 #9
0
        public LanguageMappingModel GetLanguageMappingModel(LanguagePair languageDirection, List <MappedLanguage> mappedLanguages)
        {
            var sourceLanguageCode = mappedLanguages?.FirstOrDefault(s => s.TradosCode.Equals(languageDirection.SourceCulture?.Name));

            if (sourceLanguageCode == null)
            {
                return(null);
            }

            var sourceLanguageMappings        = LanguageMappingsService.GetMTCloudLanguages(sourceLanguageCode, languageDirection.SourceCulture);
            var sourceLanguageMappingSelected = sourceLanguageMappings.FirstOrDefault(a => a.IsLocale) ?? sourceLanguageMappings[0];

            var targetLanguageCode = mappedLanguages.FirstOrDefault(s => s.TradosCode.Equals(languageDirection.TargetCulture?.Name));

            if (targetLanguageCode == null)
            {
                return(null);
            }

            var name = $"{languageDirection.SourceCulture?.DisplayName} - {languageDirection.TargetCulture?.DisplayName}";
            var savedLanguageMappingModel = Options.LanguageMappings.FirstOrDefault(a => a.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));

            var targetLanguageMappings        = LanguageMappingsService.GetMTCloudLanguages(targetLanguageCode, languageDirection.TargetCulture);
            var targetLanguageMappingSelected = targetLanguageMappings.FirstOrDefault(a => a.IsLocale) ?? targetLanguageMappings[0];

            // assign the selected target langauge
            targetLanguageMappingSelected = targetLanguageMappings.FirstOrDefault(a =>
                                                                                  a.CodeName.Equals(savedLanguageMappingModel?.SelectedTarget?.CodeName))
                                            ?? targetLanguageMappingSelected;

            var engineModels = LanguageMappingsService.GetTranslationModels(sourceLanguageMappingSelected, targetLanguageMappingSelected,
                                                                            sourceLanguageCode.TradosCode, targetLanguageCode.TradosCode);

            // attempt to recover the language model from the secondary language code if it exists!
            if (engineModels.Count == 1 && engineModels[0].DisplayName == PluginResources.Message_No_model_available && targetLanguageMappings.Count > 1 &&
                savedLanguageMappingModel?.SelectedModel.DisplayName != PluginResources.Message_No_model_available)
            {
                var secondaryLanguageCode = targetLanguageMappings.FirstOrDefault(a => a.CodeName != targetLanguageMappingSelected.CodeName);

                var secondaryEngineModels = LanguageMappingsService.GetTranslationModels(
                    sourceLanguageMappingSelected, secondaryLanguageCode, sourceLanguageCode.TradosCode, targetLanguageCode.TradosCode);

                if (secondaryEngineModels.Any())
                {
                    engineModels = secondaryEngineModels;
                    targetLanguageMappingSelected = secondaryLanguageCode;
                }
            }

            if (engineModels.Any())
            {
                // assign the selected model
                var selectedModel =
                    engineModels.FirstOrDefault(a => a.DisplayName.Equals(savedLanguageMappingModel?.SelectedModel?.DisplayName, StringComparison.InvariantCultureIgnoreCase))
                    ?? engineModels.FirstOrDefault(a => a.Model.Equals("generic", StringComparison.InvariantCultureIgnoreCase))
                    ?? engineModels[0];

                var dictionaries = LanguageMappingsService.GetDictionaries(sourceLanguageMappingSelected, targetLanguageMappingSelected);

                // assign the selected dictionary
                var selectedDictionary =
                    dictionaries.FirstOrDefault(a => a.Name.Equals(savedLanguageMappingModel?.SelectedDictionary?.Name))
                    ?? dictionaries[0];

                var languageMappingModel = new LanguageMappingModel
                {
                    Name               = name,
                    SourceLanguages    = sourceLanguageMappings,
                    TargetLanguages    = targetLanguageMappings,
                    SelectedSource     = sourceLanguageMappingSelected,
                    SelectedTarget     = targetLanguageMappingSelected,
                    SourceTradosCode   = sourceLanguageCode.TradosCode,
                    TargetTradosCode   = targetLanguageCode.TradosCode,
                    Models             = engineModels,
                    SelectedModel      = selectedModel,
                    Dictionaries       = dictionaries,
                    SelectedDictionary = selectedDictionary
                };

                return(languageMappingModel);
            }

            return(null);
        }