Esempio n. 1
0
        private void ConvertLocaleMessage(ITurnContext context, string fromLocale)
        {
            var message = context.Activity.AsMessageActivity();

            if (message != null)
            {
                if (_localeConverter.IsLocaleAvailable(fromLocale) && fromLocale != _toLocale)
                {
                    var localeConvertedText = _localeConverter.Convert(message.Text, fromLocale, _toLocale);
                    message.Text = localeConvertedText;
                }
            }
        }
 /// <summary>
 /// Constructor for developer defined detection of user messages
 /// </summary>
 /// <param name="getUserLocale">Delegate for getting the user locale</param>
 /// <param name="checkUserLocaleChanged">Delegate that returns true if the locale was changed (implements logic to change locale by intercepting the message)</param>
 /// <param name="toLocale">Target Locale</param>
 /// <param name="localeConverter">An ILocaleConverter instance</param>
 public LocaleConverterMiddleware(Func <ITurnContext, string> getUserLocale, Func <ITurnContext, Task <bool> > checkUserLocaleChanged, string toLocale, ILocaleConverter localeConverter)
 {
     _localeConverter = localeConverter ?? throw new ArgumentNullException(nameof(localeConverter));
     if (string.IsNullOrEmpty(toLocale))
     {
         throw new ArgumentNullException(nameof(toLocale));
     }
     else if (!localeConverter.IsLocaleAvailable(toLocale))
     {
         throw new ArgumentNullException("The locale " + nameof(toLocale) + " is unavailable");
     }
     _toLocale      = toLocale;
     _getUserLocale = getUserLocale ?? throw new ArgumentNullException(nameof(getUserLocale));
     _setUserLocale = checkUserLocaleChanged ?? throw new ArgumentNullException(nameof(checkUserLocaleChanged));
 }
        /// <summary>
        /// Constructor for developer defined detection of user messages
        /// </summary>
        /// <param name="userLocaleProperty">PropertyAccessor for the users preferred locale</param>
        /// <param name="toLocale">Target Locale</param>
        /// <param name="localeConverter">An ILocaleConverter instance</param>
        public LocaleConverterMiddleware(IStatePropertyAccessor <string> userLocaleProperty, string toLocale, ILocaleConverter localeConverter)
        {
            _localeConverter = localeConverter ?? throw new ArgumentNullException(nameof(localeConverter));
            if (string.IsNullOrEmpty(toLocale))
            {
                throw new ArgumentNullException(nameof(toLocale));
            }
            else if (!localeConverter.IsLocaleAvailable(toLocale))
            {
                throw new ArgumentNullException("The locale " + nameof(toLocale) + " is unavailable");
            }

            _toLocale           = toLocale;
            _userLocaleProperty = userLocaleProperty ?? throw new ArgumentNullException(nameof(userLocaleProperty));
        }