Exemple #1
0
        private Activity InternalGenerateActivity(string templateName, object data, string locale)
        {
            var iLocale = locale == null ? "" : locale;

            if (TemplateEnginesPerLocale.ContainsKey(iLocale))
            {
                return(ActivityFactory.FromObject(TemplateEnginesPerLocale[locale].Evaluate(templateName, data)));
            }
            var locales = new string[] { string.Empty };

            if (!LangFallBackPolicy.TryGetValue(iLocale, out locales))
            {
                if (!LangFallBackPolicy.TryGetValue(string.Empty, out locales))
                {
                    throw new Exception($"No supported language found for {iLocale}");
                }
            }

            foreach (var fallBackLocale in locales)
            {
                if (TemplateEnginesPerLocale.ContainsKey(fallBackLocale))
                {
                    return(ActivityFactory.FromObject(TemplateEnginesPerLocale[fallBackLocale].Evaluate(templateName, data)));
                }
            }
            return(new Activity());
        }
Exemple #2
0
        public static Func <string, ImportResolverDelegate> MultiLanguageResolverDelegate(ResourceExplorer resourceExplorer) =>
        (string targetLocale) =>
        (string source, string id) =>
        {
            var languagePolicy = new LanguagePolicy();

            var locales = new string[] { string.Empty };
            if (!languagePolicy.TryGetValue(targetLocale, out locales))
            {
                if (!languagePolicy.TryGetValue(string.Empty, out locales))
                {
                    throw new Exception($"No supported language found for {targetLocale}");
                }
            }

            var resourceName = Path.GetFileName(PathUtils.NormalizePath(id));

            foreach (var locale in locales)
            {
                var resourceId = string.IsNullOrEmpty(locale) ? resourceName : resourceName.Replace(".lg", $".{locale}.lg");

                if (resourceExplorer.TryGetResource(resourceId, out var resource))
                {
                    var content = resource.ReadTextAsync().GetAwaiter().GetResult();

                    return(content, resourceName);
                }
            }

            return(string.Empty, resourceName);
        };
        /// <summary>
        /// Create an activity through Language Generation using the thread culture or provided override.
        /// </summary>
        /// <param name="templateName">Langauge Generation template.</param>
        /// <param name="data">Data for Language Generation to use during response generation.</param>
        /// <param name="localeOverride">Optional override for locale.</param>
        /// <returns>Activity.</returns>
        /// <remarks>
        /// The InputHint property of the returning activity is set to be null if it's acceptingInput so
        /// when the activity is being used in a prompt it'll be set to expectingInput.
        /// </remarks>
        public Activity GenerateActivityForLocale(string templateName, object data = null, string localeOverride = null)
        {
            if (templateName == null)
            {
                throw new ArgumentNullException(nameof(templateName));
            }

            // By default we use the locale for the current culture, if a locale is provided then we ignore this.
            var locale = localeOverride ?? CultureInfo.CurrentUICulture.Name;

            // Do we have a template engine for this locale?
            if (TemplateEnginesPerLocale.ContainsKey(locale))
            {
                var activity = ActivityFactory.CreateActivity(TemplateEnginesPerLocale[locale].EvaluateTemplate(templateName, data).ToString());

                // Set the inputHint to null when it's acceptingInput so prompt can override it when expectingInput
                if (activity.InputHint == InputHints.AcceptingInput)
                {
                    activity.InputHint = null;
                }

                return(activity);
            }
            else
            {
                // We don't have a set of matching responses for this locale so we apply fallback policy to find options.
                languageFallbackPolicy.TryGetValue(locale, out string[] locales);
                {
                    // If no fallback options were found then we fallback to the default and log.
                    if (!languageFallbackPolicy.TryGetValue(localeDefault, out locales))
                    {
                        throw new Exception($"No LG responses found for {locale} or when attempting to fallback to '{localeDefault}'");
                    }
                }

                // Work through the fallback hierarchy to find a response
                foreach (var fallBackLocale in locales)
                {
                    if (TemplateEnginesPerLocale.ContainsKey(fallBackLocale))
                    {
                        var activity = ActivityFactory.CreateActivity(TemplateEnginesPerLocale[fallBackLocale].EvaluateTemplate(templateName, data).ToString());

                        // Set the inputHint to null when it's acceptingInput so prompt can override it when expectingInput
                        if (activity.InputHint == InputHints.AcceptingInput)
                        {
                            activity.InputHint = null;
                        }

                        return(activity);
                    }
                }
            }

            throw new Exception($"No LG responses found for {locale} or when attempting to fallback to '{localeDefault}'");
        }
Exemple #4
0
        public override async Task <RecognizerResult> RecognizeAsync(DialogContext dialogContext, Activity activity, CancellationToken cancellationToken = default, Dictionary <string, string> telemetryProperties = null, Dictionary <string, double> telemetryMetrics = null)
        {
            var policy = new List <string>();

            if (LanguagePolicy.TryGetValue(activity.Locale, out string[] targetpolicy))
 public override async Task <RecognizerResult> RecognizeAsync(DialogContext dialogContext, Activity activity, CancellationToken cancellationToken)
 {
     if (!LanguagePolicy.TryGetValue(activity.Locale ?? string.Empty, out string[] policy))
Exemple #6
0
 public Task <RecognizerResult> RecognizeAsync(ITurnContext turnContext, CancellationToken cancellationToken)
 {
     if (!LanguagePolicy.TryGetValue(turnContext.Activity.Locale ?? string.Empty, out string[] policy))