AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware
1.0.0
dotnet add package AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware --version 1.0.0
NuGet\Install-Package AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware -Version 1.0.0
<PackageReference Include="AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware" Version="1.0.0" />
<PackageVersion Include="AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware" Version="1.0.0" />
<PackageReference Include="AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware" />
paket add AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware --version 1.0.0
#r "nuget: AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware, 1.0.0"
#:package AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware@1.0.0
#addin nuget:?package=AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware&version=1.0.0
#tool nuget:?package=AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware&version=1.0.0
<div align="center">
π AzureAICommunity β Language Translation Middleware (.NET)
Automatic language detection, translation, and response back-translation middleware for AI agent applications built on Microsoft.Extensions.AI.
**Your agent always
</div>
Overview
AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware is a plug-and-play translation layer for Microsoft.Extensions.AI chat pipelines. It automatically detects the user's language, translates incoming messages to the agent's target language, and back-translates the response β using Azure AI Translator with an optional LLM agent as fallback.
β¨ Features
| Feature | |
|---|---|
| π | Language detection β automatically detect the user's language with configurable confidence threshold |
| π | Round-trip translation β translate incoming messages and back-translate agent responses |
| βοΈ | Azure Translator β production-grade speed and accuracy with 100+ languages |
| π€ | LLM fallback β silently falls back to LLM translation when Azure is unavailable |
| π§ | Builder pattern β fluent CreateBuilder() API for easy composition |
| π | Streaming support β works with both standard and streaming IChatClient calls |
| π | Drop-in middleware β integrates directly into any Microsoft.Extensions.AI pipeline |
π¦ Installation
dotnet add package AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware
π Quick Start
using AzureAICommunity.Agent.Middleware.LanguageTranslationMiddleware;
using Microsoft.Extensions.AI;
using OllamaSharp;
IChatClient ollamaClient = new OllamaApiClient(new Uri("http://localhost:11434/"), "llama3.2");
// LLM-only translation (no Azure credentials required)
var client = ollamaClient
.AsBuilder()
.Use(inner => LanguageTranslationMiddleware
.CreateBuilder()
.WithTargetLanguage("en")
.WithLLMFallback(ollamaClient)
.Build(inner))
.Build();
var messages = new[] { new ChatMessage(ChatRole.User, "Wie heiΓt die Hauptstadt von Frankreich?") };
var response = await client.GetResponseAsync(messages);
Console.WriteLine(response.Messages[0].Text); // Reply back-translated to German
βοΈ Configuration
| Parameter | Method | Default | Description |
|---|---|---|---|
targetLanguage |
WithTargetLanguage(string) |
"en" |
ISO 639-1 code of the language the agent reasons in |
minConfidence |
WithMinConfidence(float) |
0.8 |
Minimum detection confidence to apply translation |
azureConfig |
WithAzureTranslator(config) |
null |
Azure Translator credentials (optional) |
llmClient |
WithLLMFallback(client) |
null |
LLM client used as translation fallback or primary service |
π§βπ» Usage
Option 1 β Azure Translator + LLM Fallback (Recommended)
var azureConfig = new AzureTranslatorConfig(
Key: Environment.GetEnvironmentVariable("AZURE_TRANSLATOR_KEY")!,
Region: Environment.GetEnvironmentVariable("AZURE_TRANSLATOR_REGION")!
);
var client = ollamaClient
.AsBuilder()
.Use(inner => LanguageTranslationMiddleware
.CreateBuilder()
.WithAzureTranslator(azureConfig)
.WithLLMFallback(ollamaClient)
.WithTargetLanguage("en")
.WithMinConfidence(0.8f)
.Build(inner))
.Build();
Option 2 β LLM Only
var client = ollamaClient
.AsBuilder()
.Use(inner => LanguageTranslationMiddleware
.CreateBuilder()
.WithLLMFallback(ollamaClient)
.WithTargetLanguage("en")
.Build(inner))
.Build();
Option 3 β Direct Construction
var azureService = new AzureTranslationService(azureConfig);
var llmService = new LLMTranslationService(ollamaClient);
var middleware = new LanguageTranslationMiddleware(
inner: ollamaClient,
primaryService: azureService,
targetLanguage: "en",
minConfidence: 0.8f,
fallbackService: llmService);
π How It Works
User Message (any language)
β
βΌ
βββββββββββββββββββββββββββββββββββ
β LanguageTranslationMiddleware β
β 1. Detect language β
β 2. Translate β target lang β
ββββββββββββββββ¬βββββββββββββββββββ
β
βΌ
Inner IChatClient
(reasons in target lang)
β
βΌ
βββββββββββββββββββββββββββββββββββ
β LanguageTranslationMiddleware β
β 3. Back-translate response β
β β user's original language β
βββββββββββββββββββββββββββββββββββ
β
βΌ
Response (user's language)
π€ Contributing
Contributions are welcome! Please open an issue to discuss what you'd like to change before submitting a pull request.
π Repository: https://github.com/rvinothrajendran/AgentFramework
- Fork the repository
- Create a feature branch (
git checkout -b feature/my-feature) - Commit your changes (
git commit -m 'Add my feature') - Push to the branch (
git push origin feature/my-feature) - Open a Pull Request
π€ Author
Built and maintained by Vinoth Rajendran.
- π GitHub: github.com/rvinothrajendran β follow for more projects!
- πΊ YouTube: youtube.com/@VinothRajendran β subscribe for tutorials and demos!
- πΌ LinkedIn: linkedin.com/in/rvinothrajendran β let's connect!
π License
MIT Β© 2026 Vinoth Rajendran β AzureAICommunity
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Azure.AI.Translation.Text (>= 1.0.0)
- Microsoft.Extensions.AI (>= 10.4.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 170 | 4/20/2026 |