Adding Translation to Bot Middleware
See original GitHub issueHi there,
Am trying to extend Bot Composer’s .NET runtime scripts to add Translation to the Middleware pipeline as shown in https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/17.multilingual-bot.
I’ve lifted the Translator folder in it’s entirety from https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/17.multilingual-bot/Translation and adapted all namespaces/references appropriately.
I made additions to the Startup.cs file in Composer’s runtime\dotnet\azurewebapp\Startup.cs:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.IO;
.....
using Microsoft.BotFramework.Composer.WebAppTemplates.Translation; // <--------------- NEW
....
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
...
// Create the Microsoft Translator responsible for making calls to the Cognitive Services translation service
services.AddSingleton<MicrosoftTranslator>(); // <--------------- NEW
// Create the Translation Middleware that will be added to the middleware pipeline in the AdapterWithErrorHandler
services.AddSingleton<TranslationMiddleware>(); // <--------------- NEW
...
var storage = ConfigureStorage(settings);
services.AddSingleton(storage);
var userState = new UserState(storage);
var conversationState = new ConversationState(storage);
services.AddSingleton(userState);
services.AddSingleton(conversationState);
// Add Translation
var translator = new MicrosoftTranslator(this.Configuration); // <--------------- NEW
var translationMiddleware = new TranslationMiddleware(translator, userState); // <--------------- NEW
// Configure bot loading path
var botDir = settings.Bot;
var resourceExplorer = new ResourceExplorer().AddFolder(botDir);
var rootDialog = GetRootDialog(botDir);
var defaultLocale = Configuration.GetValue<string>("defaultLocale") ?? "en-us";
services.AddSingleton(resourceExplorer);
// Add translationMiddleware instance to Adapter
services.AddSingleton<IBotFrameworkHttpAdapter, BotFrameworkHttpAdapter>((s) => GetBotAdapter(storage, settings, userState, conversationState, s, s.GetService<TelemetryInitializerMiddleware>(), translationMiddleware)); // <--------------- NEW
// Guessing I should be passing translationMiddleware to this ComposerBot instance too??
services.AddSingleton<IBot>(s =>
new ComposerBot(
s.GetService<ConversationState>(),
s.GetService<UserState>(),
s.GetService<ResourceExplorer>(),
s.GetService<BotFrameworkClient>(),
s.GetService<SkillConversationIdFactoryBase>(),
s.GetService<IBotTelemetryClient>(),
// Add translationMiddleware here??
rootDialog,
defaultLocale));
}
I’m able to successfully deploy this Composer bot to Azure and interact with it (assuming this means the code above is so far working as intended), though the Translator instance isn’t being used by the bot just yet. It’s been difficult to lift + shift code from the C# bot github from this point onwards, as Composer’s bot logic setup seems a little different.
Wondering if you can provide any guidance on this or give me a sense of just how much work is involved - how much more do I need to configure to add Translation to the bot?
Cheers
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (4 by maintainers)
Top GitHub Comments
@ku222 , we don’t have a sample for now, I will try to add one in composer and let you know when it is ready.
@ku222 I’m planning to implement Translation same like you. But I’m getting an error when trying to add package - Microsoft.BotFramework.Composer.WebAppTemplates.Translation
I’ve described my issue more clearly here in stackoverflow: https://stackoverflow.com/q/70412469/17716558
Can you please help me where to find that package and how to remove “No package exists” error?