private string DetermineCulture(DialogContext dc, FindChoicesOptions opt = null)
        {
            // Note: opt.Locale and Default locale will be considered for deprecation as part of 4.13.
            var candidateLocale = dc.GetLocale() ?? opt?.Locale ?? DefaultLocale?.GetValue(dc.State);
            var culture         = PromptCultureModels.MapToNearestLanguage(candidateLocale);

            if (string.IsNullOrEmpty(culture) || !DefaultChoiceOptions.ContainsKey(culture))
            {
                culture = English;
            }

            return(culture);
        }
Exemple #2
0
        /// <summary>
        /// Method to generate text from given template and data.
        /// </summary>
        /// <param name="dialogContext">Context for the current turn of conversation.</param>
        /// <param name="template">template to evaluate.</param>
        /// <param name="data">data to bind to.</param>
        /// <param name="cancellationToken">the <see cref="CancellationToken"/> for the task.</param>
        /// <returns>generated text.</returns>
        public override Task <object> GenerateAsync(DialogContext dialogContext, string template, object data, CancellationToken cancellationToken = default)
        {
            var lgOpt = new EvaluationOptions()
            {
                Locale = dialogContext.GetLocale()
            };

            try
            {
                return(Task.FromResult(lg.EvaluateText(template, data, lgOpt)));
            }
            catch (Exception err)
            {
                if (!string.IsNullOrEmpty(this.Id))
                {
                    throw new Exception($"{Id}:{err.Message}");
                }

                throw;
            }
        }
        /// <summary>
        /// Method to generate text from given template and data.
        /// </summary>
        /// <param name="dialogContext">Context for the current turn of conversation.</param>
        /// <param name="template">template to evaluate.</param>
        /// <param name="data">data to bind to.</param>
        /// <param name="cancellationToken">the <see cref="CancellationToken"/> for the task.</param>
        /// <returns>generated text.</returns>
        public override Task <object> GenerateAsync(DialogContext dialogContext, string template, object data, CancellationToken cancellationToken = default)
        {
            EventHandler onEvent = (s, e) => RunSync(() => HandlerLGEventAsync(dialogContext, s, e, cancellationToken));

            var lgOpt = new EvaluationOptions()
            {
                Locale = dialogContext.GetLocale(), OnEvent = onEvent
            };

            try
            {
                return(Task.FromResult(lg.EvaluateText(template, data, lgOpt)));
            }
            catch (Exception err)
            {
                if (!string.IsNullOrEmpty(this.Id))
                {
                    throw new InvalidOperationException($"{Id}:{err.Message}");
                }

                throw;
            }
        }
        /// <summary>
        /// Method to generate text from given template and data.
        /// </summary>
        /// <param name="dialogContext">Context for the current turn of conversation.</param>
        /// <param name="template">template to evaluate.</param>
        /// <param name="data">data to bind to.</param>
        /// <param name="cancellationToken">the <see cref="CancellationToken"/> for the task.</param>
        /// <returns>generated text.</returns>
        public override async Task <object> GenerateAsync(DialogContext dialogContext, string template, object data, CancellationToken cancellationToken = default)
        {
            var lgOpt = new EvaluationOptions()
            {
                Locale = dialogContext.GetLocale()
            };

            try
            {
                var lg = await _lg.Value.ConfigureAwait(false);

                return(lg.EvaluateText(template, data, lgOpt));
            }
            catch (Exception err)
            {
                if (!string.IsNullOrEmpty(this.Id))
                {
                    throw new InvalidOperationException($"{Id}:{err.Message}");
                }

                throw;
            }
        }
        /// <summary>
        /// Find a language generator that matches the current context locale.
        /// </summary>
        /// <param name="dialogContext">Context for the current turn of conversation.</param>
        /// <param name="template">The template.</param>
        /// <param name="data">data to bind to.</param>
        /// <param name="cancellationToken">the <see cref="CancellationToken"/> for the task.</param>
        /// <returns>The generator.</returns>
        public override async Task <object> GenerateAsync(DialogContext dialogContext, string template, object data, CancellationToken cancellationToken = default)
        {
            // priority
            // 1. local policy
            // 2. shared policy in turnContext
            // 3. default policy
            var languagePolicy = this.LanguagePolicy ??
                                 dialogContext.Services.Get <LanguagePolicy>() ??
                                 new LanguagePolicy();

            // see if we have any locales that match
            var fallbackLocales = new List <string>();
            var targetLocale    = dialogContext.GetLocale();

            if (languagePolicy.ContainsKey(targetLocale))
            {
                fallbackLocales.AddRange(languagePolicy[targetLocale]);
            }

            // append empty as fallback to end
            if (targetLocale.Length != 0 && languagePolicy.ContainsKey(string.Empty))
            {
                fallbackLocales.AddRange(languagePolicy[string.Empty]);
            }

            if (fallbackLocales.Count == 0)
            {
                throw new InvalidOperationException($"No supported language found for {targetLocale}");
            }

            var generators = new List <Lazy <LanguageGenerator> >();

            foreach (var locale in fallbackLocales)
            {
                if (this.TryGetGenerator(dialogContext, locale, out Lazy <LanguageGenerator> generator))
                {
                    generators.Add(generator);
                }
            }

            if (generators.Count == 0)
            {
                throw new InvalidOperationException($"No generator found for language {targetLocale}");
            }

            var errors = new List <string>();

            foreach (var generator in generators)
            {
                try
                {
                    return(await generator.Value.GenerateAsync(dialogContext, template, data, cancellationToken).ConfigureAwait(false));
                }
#pragma warning disable CA1031 // Do not catch general exception types (catch any exception and add it to the errors list).
                catch (Exception err)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    errors.Add(err.Message);
                }
            }

            throw new InvalidOperationException(string.Join(",\n", errors.Distinct()));
        }
 private string GetCulture(DialogContext dc)
 {
     // Note: Default locale will be considered for deprecation as part of 4.13.
     return(dc.GetLocale() ?? DefaultLocale?.GetValue(dc.State) ?? string.Empty);
 }