コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TranslationMiddleware"/> class.
        /// </summary>
        /// <param name="nativeLanguages">The languages supported by your app.</param>
        /// <param name="translatorKey">Your subscription key for the Microsoft Translator Text API.</param>
        /// <param name="toUserLanguage">Indicates whether to translate messages sent from the bot into the user's language.</param>
        /// <param name="httpClient">An alternate HTTP client to use.</param>
        /// <param name="defaultLocale">Default locale to use when underlying user locale is undefined.</param>
        public TranslationMiddleware(string[] nativeLanguages, string translatorKey, bool toUserLanguage = false, HttpClient httpClient = null, string defaultLocale = "en")
        {
            if (string.IsNullOrWhiteSpace(defaultLocale))
            {
                throw new ArgumentNullException(nameof(defaultLocale));
            }

            AssertValidNativeLanguages(nativeLanguages);
            this._nativeLanguages = nativeLanguages;
            if (string.IsNullOrEmpty(translatorKey))
            {
                throw new ArgumentNullException(nameof(translatorKey));
            }

            this._translator = new Translator(translatorKey, httpClient);
            _patterns        = new Dictionary <string, List <string> >();
            _userConfiguredLanguageDictionary = new ConfiguredLanguageDictionary();
            _toUserLanguage = toUserLanguage;
            DefaultLocale   = defaultLocale;
        }
コード例 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Translator"/> class.
 /// </summary>
 /// <param name="apiKey">Your subscription key for the Microsoft Translator Text API.</param>
 /// <param name="patterns">List of regex patterns, indexed by language identifier,
 /// that can be used to flag text that should not be translated.</param>
 /// /// <param name="userConfiguredLanguageDictionary">Custom languages dictionary object, used to store all the different languages dictionaries
 /// configured by the user to overwrite the translator output to certain vocab by the custom dictionary translation.</param>
 /// <param name="httpClient">An alternate HTTP client to use.</param>
 public Translator(string apiKey, Dictionary <string, List <string> > patterns, ConfiguredLanguageDictionary userConfiguredLanguageDictionary, HttpClient httpClient = null)
     : this(apiKey, httpClient)
 {
     InitializePostProcessors(patterns, userConfiguredLanguageDictionary);
 }
コード例 #3
0
        /// <summary>
        /// Initialize attached post processors according to the availability of patterns and custom dictionary provided to the translator constructor.
        /// </summary>
        private void InitializePostProcessors(Dictionary <string, List <string> > patterns, ConfiguredLanguageDictionary userConfiguredLanguageDictionary)
        {
            attachedPostProcessors = new List <IPostProcessor>();
            if (patterns != null && patterns.Count > 0)
            {
                attachedPostProcessors.Add(new PatternsPostProcessor(patterns));
            }

            if (userConfiguredLanguageDictionary != null && !userConfiguredLanguageDictionary.IsEmpty())
            {
                attachedPostProcessors.Add(new ConfiguredLanguageDictionaryPostProcessor(userConfiguredLanguageDictionary));
            }
        }
コード例 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TranslationMiddleware"/> class.
 /// </summary>
 /// <param name="nativeLanguages">The languages supported by your app.</param>
 /// <param name="translatorKey">Your subscription key for the Microsoft Translator Text API.</param>
 /// <param name="patterns">List of regex patterns, indexed by language identifier,
 /// that can be used to flag text that should not be translated.</param>
 /// /// <param name="userConfiguredLanguageDictionary">Custom languages dictionary object, used to store all the different languages dictionaries
 /// configured by the user to overwrite the translator output to certain vocab by the custom dictionary translation.</param>
 /// <param name="toUserLanguage">Indicates whether to translate messages sent from the bot into the user's language.</param>
 /// <param name="defaultLocale">Default locale to use when underlying user locale is undefined.</param>
 /// <remarks>Each pattern the <paramref name="patterns"/> describes an entity that should not be translated.
 /// For example, in French <c>je m’appelle ([a-z]+)</c>, which will avoid translation of anything coming after je m’appelle.</remarks>
 /// <param name="httpClient">An alternate HTTP client to use.</param>
 public TranslationMiddleware(string[] nativeLanguages, string translatorKey, Dictionary <string, List <string> > patterns, ConfiguredLanguageDictionary userConfiguredLanguageDictionary, bool toUserLanguage = false, HttpClient httpClient = null, string defaultLocale = "en")
     : this(nativeLanguages, translatorKey, toUserLanguage, httpClient, defaultLocale)
 {
     this._translator = new Translator(translatorKey, patterns, userConfiguredLanguageDictionary, httpClient);
 }