11# Copyright (c) Microsoft Corporation. All rights reserved.
22# Licensed under the MIT License.
33
4- from typing import Callable , Dict , List , Union
4+ from typing import Callable , Dict , List
55
6- from recognizers_text import Culture
76from botbuilder .core import TurnContext
87from botbuilder .dialogs .choices import (
98 Choice ,
1514from botbuilder .schema import Activity , ActivityTypes
1615
1716from .prompt import Prompt
17+ from .prompt_culture_models import PromptCultureModels
1818from .prompt_options import PromptOptions
1919from .prompt_validator_context import PromptValidatorContext
2020from .prompt_recognizer_result import PromptRecognizerResult
@@ -29,69 +29,43 @@ class ChoicePrompt(Prompt):
2929 """
3030
3131 _default_choice_options : Dict [str , ChoiceFactoryOptions ] = {
32- Culture . Spanish : ChoiceFactoryOptions (
33- inline_separator = ", " ,
34- inline_or = " o " ,
35- inline_or_more = ", o " ,
32+ c . locale : ChoiceFactoryOptions (
33+ inline_separator = c . separator ,
34+ inline_or = c . inline_or_more ,
35+ inline_or_more = c . inline_or_more ,
3636 include_numbers = True ,
37- ),
38- Culture .Dutch : ChoiceFactoryOptions (
39- inline_separator = ", " ,
40- inline_or = " of " ,
41- inline_or_more = ", of " ,
42- include_numbers = True ,
43- ),
44- Culture .English : ChoiceFactoryOptions (
45- inline_separator = ", " ,
46- inline_or = " or " ,
47- inline_or_more = ", or " ,
48- include_numbers = True ,
49- ),
50- Culture .French : ChoiceFactoryOptions (
51- inline_separator = ", " ,
52- inline_or = " ou " ,
53- inline_or_more = ", ou " ,
54- include_numbers = True ,
55- ),
56- "de-de" : ChoiceFactoryOptions (
57- inline_separator = ", " ,
58- inline_or = " oder " ,
59- inline_or_more = ", oder " ,
60- include_numbers = True ,
61- ),
62- Culture .Japanese : ChoiceFactoryOptions (
63- inline_separator = "、 " ,
64- inline_or = " または " ,
65- inline_or_more = "、 または " ,
66- include_numbers = True ,
67- ),
68- Culture .Portuguese : ChoiceFactoryOptions (
69- inline_separator = ", " ,
70- inline_or = " ou " ,
71- inline_or_more = ", ou " ,
72- include_numbers = True ,
73- ),
74- Culture .Chinese : ChoiceFactoryOptions (
75- inline_separator = ", " ,
76- inline_or = " 要么 " ,
77- inline_or_more = ", 要么 " ,
78- include_numbers = True ,
79- ),
37+ )
38+ for c in PromptCultureModels .get_supported_cultures ()
8039 }
8140
8241 def __init__ (
8342 self ,
8443 dialog_id : str ,
8544 validator : Callable [[PromptValidatorContext ], bool ] = None ,
8645 default_locale : str = None ,
46+ choice_defaults : Dict [str , ChoiceFactoryOptions ] = None ,
8747 ):
48+ """
49+ :param dialog_id: Unique ID of the dialog within its parent `DialogSet`.
50+ :param validator: (Optional) validator that will be called each time the user responds to the prompt.
51+ If the validator replies with a message no additional retry prompt will be sent.
52+ :param default_locale: (Optional) locale to use if `dc.context.activity.locale` not specified.
53+ Defaults to a value of `en-us`.
54+ :param choice_defaults: (Optional) Overrides the dictionary of
55+ Bot Framework SDK-supported _default_choice_options.
56+ As type Dict[str, ChoiceFactoryOptions], the key is a string of the locale, such as "en-us".
57+ * Must be passed in to each ConfirmPrompt that needs the custom choice defaults.
58+ """
8859 super ().__init__ (dialog_id , validator )
8960
9061 self .style = ListStyle .auto
9162 self .default_locale = default_locale
9263 self .choice_options : ChoiceFactoryOptions = None
9364 self .recognizer_options : FindChoicesOptions = None
9465
66+ if choice_defaults is not None :
67+ self ._default_choice_options = choice_defaults
68+
9569 async def on_prompt (
9670 self ,
9771 turn_context : TurnContext ,
@@ -106,20 +80,15 @@ async def on_prompt(
10680 raise TypeError ("ChoicePrompt.on_prompt(): options cannot be None." )
10781
10882 # Determine culture
109- culture : Union [
110- str , None
111- ] = turn_context .activity .locale if turn_context .activity .locale else self .default_locale
112-
113- if not culture or culture not in ChoicePrompt ._default_choice_options :
114- culture = Culture .English
83+ culture = self ._determine_culture (turn_context .activity )
11584
11685 # Format prompt to send
11786 choices : List [Choice ] = options .choices if options .choices else []
11887 channel_id : str = turn_context .activity .channel_id
11988 choice_options : ChoiceFactoryOptions = (
12089 self .choice_options
12190 if self .choice_options
122- else ChoicePrompt ._default_choice_options [culture ]
91+ else self ._default_choice_options [culture ]
12392 )
12493 choice_style = (
12594 0 if options .style == 0 else options .style if options .style else self .style
@@ -155,15 +124,25 @@ async def on_recognize(
155124 if not utterance :
156125 return result
157126 opt : FindChoicesOptions = self .recognizer_options if self .recognizer_options else FindChoicesOptions ()
158- opt .locale = (
159- activity .locale
160- if activity .locale
161- else (self .default_locale or Culture .English )
162- )
127+ opt .locale = self ._determine_culture (turn_context .activity , opt )
163128 results = ChoiceRecognizers .recognize_choices (utterance , choices , opt )
164129
165130 if results is not None and results :
166131 result .succeeded = True
167132 result .value = results [0 ].resolution
168133
169134 return result
135+
136+ def _determine_culture (
137+ self , activity : Activity , opt : FindChoicesOptions = FindChoicesOptions ()
138+ ) -> str :
139+ culture = (
140+ PromptCultureModels .map_to_nearest_language (activity .locale )
141+ or opt .locale
142+ or self .default_locale
143+ or PromptCultureModels .English .locale
144+ )
145+ if not culture or not self ._default_choice_options .get (culture ):
146+ culture = PromptCultureModels .English .locale
147+
148+ return culture
0 commit comments