LocaleFinder.cs
1 using System.Threading;
2 using System.Threading.Tasks;
7 
8 namespace Microsoft.Bot.Builder.ConnectorEx
9 {
10 
14  public interface ILocaleFinder
15  {
22  Task<string> FindLocale(IActivity activity, CancellationToken token);
23  }
24 
28  public sealed class LocaleFinder : ILocaleFinder
29  {
30  private readonly ResumptionContext resumptionContext;
31  private readonly ConversationReference conversationReference;
32 
33  public LocaleFinder(ConversationReference conversationReference, ResumptionContext resumptionContext)
34  {
35  SetField.NotNull(out this.conversationReference, nameof(conversationReference), conversationReference);
36  SetField.NotNull(out this.resumptionContext, nameof(resumptionContext), resumptionContext);
37  }
38 
39  public async Task<string> FindLocale(IActivity activity, CancellationToken token)
40  {
41  var resumptionData = await this.resumptionContext.LoadDataAsync(token);
42 
43  if (resumptionData != null && resumptionData.IsTrustedServiceUrl)
44  {
45  MicrosoftAppCredentials.TrustServiceUrl(this.conversationReference.ServiceUrl);
46  }
47 
48  var locale = (activity as IMessageActivity)?.Locale;
49 
50  // if locale is null or whitespace in the incoming request,
51  // try to set it from the ResumptionContext
52  if (string.IsNullOrWhiteSpace(locale))
53  {
54  locale = resumptionData?.Locale;
55  }
56 
57  // persist resumptionData with updated information
58  var data = new ResumptionData
59  {
60  Locale = locale,
61  IsTrustedServiceUrl = MicrosoftAppCredentials.IsTrustedServiceUrl(this.conversationReference.ServiceUrl)
62  };
63  await this.resumptionContext.SaveDataAsync(data, token);
64  return locale;
65  }
66  }
67 }
async Task< string > FindLocale(IActivity activity, CancellationToken token)
Given an activity it finds the locale.
Definition: LocaleFinder.cs:39
Namespace for the Microsoft Bot Connector SDK.
static void TrustServiceUrl(string serviceUrl, DateTime expirationTime=default(DateTime))
Adds the host of service url to MicrosoftAppCredentials trusted hosts.
The data persisted for ConversationReference that will be consumed for Conversation.ResumeAsync.
Namespace for internal machinery that is not useful for most developers and may change in the future...
The resumption context that is responsible for loading/persisting the ResumptionData.
An object relating to a particular point in a conversation
The interface for finding and setting locale for LocalizedScope in SetAmbientThreadCulture.
Definition: LocaleFinder.cs:14
LocaleFinder(ConversationReference conversationReference, ResumptionContext resumptionContext)
Definition: LocaleFinder.cs:33
Namespace for internal Dialogs machinery that is not useful for most developers and may change in the...
Shared properties for all activities
Definition: IActivity.cs:9
Namespace for the internal fibers machinery that is not useful for most developers and may change in ...
Core namespace for Dialogs and associated infrastructure.
static bool IsTrustedServiceUrl(string serviceUrl)
Checks if the service url is for a trusted host or not.
The locale finder implementation based on ResumptionContext.
Definition: LocaleFinder.cs:28
Root namespace for the Microsoft Bot Builder SDK.