question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding Translation to Bot Middleware

See original GitHub issue

Hi 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:closed
  • Created 3 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
luhan2017commented, Jun 30, 2020

@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.

0reactions
SuryaKorivipaduPracticecommented, Dec 20, 2021

@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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add Translation Middleware in Bot-Framework ...
I'm using v2.1.1 and when I create a new bot, the bot is set to en-us automatically. After that i have added Indonesian...
Read more >
Solved: Language Translation Middleware Support
So I was wondering if we can add middlewares, like a normal bot composer bot, to translate any utterance to english (if detected...
Read more >
15 - Custom Middleware in Bot composer - YouTube
In this video explain about how to write the custom middleware. Middleware is simply a class that sits between the adapter and your...
Read more >
How to use the botbuilder-ai.LanguageTranslator function in ...
resolve(true); } else { return Promise.resolve(false); } } // Add language translator middleware const languageTranslator = new LanguageTranslator({ ...
Read more >
The Babel-bot: Make your bot fluent in every language | Botfuel
async (context, next, done) => { /** middleware OUT 1 */ }, ], };. In this article, we are going to use the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found