Azure.AI.Translation.Text
2.0.0-beta.1
Prefix Reserved
dotnet add package Azure.AI.Translation.Text --version 2.0.0-beta.1
NuGet\Install-Package Azure.AI.Translation.Text -Version 2.0.0-beta.1
<PackageReference Include="Azure.AI.Translation.Text" Version="2.0.0-beta.1" />
<PackageVersion Include="Azure.AI.Translation.Text" Version="2.0.0-beta.1" />
<PackageReference Include="Azure.AI.Translation.Text" />
paket add Azure.AI.Translation.Text --version 2.0.0-beta.1
#r "nuget: Azure.AI.Translation.Text, 2.0.0-beta.1"
#:package Azure.AI.Translation.Text@2.0.0-beta.1
#addin nuget:?package=Azure.AI.Translation.Text&version=2.0.0-beta.1&prerelease
#tool nuget:?package=Azure.AI.Translation.Text&version=2.0.0-beta.1&prerelease
Azure Text Translation client library for .NET
Azure text translation is a cloud-based REST API provided by the Azure Translator service. It utilizes neural machine translation technology to deliver precise, contextually relevant, and semantically accurate real-time text translations across all supported languages.
Use the Text Translation client library for .NET to:
Retrieve the list of languages supported for translation and transliteration operations, as well as LLM models available for translations.
Perform deterministic text translation from a specified source language to a target language, with configurable parameters to ensure precision and maintain contextual integrity.
Execute transliteration by converting text from the original script to an alternative script representation.
Use LLM models to produce translation output variants that are tone-specific and gender-aware.
Source code | API reference documentation | Product documentation
Getting started
Install the package
Install the Azure Text Translation client library for .NET with NuGet:
dotnet add package Azure.AI.Translation.Text --prerelease
This table shows the relationship between SDK versions and supported API versions of the service:
| SDK version | Supported API version of service |
|---|---|
| 1.0.0-beta.1 | 3.0 |
| 1.0.0 | 3.0 |
| 2.0.0-beta.1 | 2025-10-01-preview |
Prerequisites
- An Azure subscription.
- An existing Translator service or Cognitive Services resource. You can create Translator resource following Create a Translator resource.
Authenticate the client
Interaction with the service using the client library begins with creating an instance of the TextTranslationClient class. You will need an API key or TokenCredential to instantiate a client object. For more information regarding authenticating with Cognitive Services, see Authenticate requests to Translator Service.
Get an API key
You can get the endpoint, API key and Region from the Cognitive Services resource or Translator service resource information in the Azure Portal.
Alternatively, use the Azure CLI snippet below to get the API key from the Translator service resource.
az cognitiveservices account keys list --resource-group <your-resource-group-name> --name <your-resource-name>
Create a TextTranslationClient using an API key and Region credential
Once you have the value for the API key and Region, create an AzureKeyCredential. This will allow you to
update the API key without creating a new client.
With the value of the endpoint, AzureKeyCredential and a Region, you can create the TextTranslationClient:
string endpoint = "<Text Translator Resource Endpoint>";
string apiKey = "<Text Translator Resource API Key>";
string region = "<Text Translator Azure Region>";
TextTranslationClient client = new TextTranslationClient(new AzureKeyCredential(apiKey), new Uri(endpoint), region);
Create TextTranslationClient with Microsoft Entra ID
Client API key authentication is used in most of the examples, but you can also authenticate with Microsoft Entra ID using the Azure Identity library. To use the DefaultAzureCredential provider shown below, install the Azure.Identity package:
dotnet add package Azure.Identity
Create a custom subdomain for your resource in order to use this type of authentication. Use this value for the endpoint variable for Text Translator Custom Endpoint.
You will also need to register a new Microsoft Entra application and grant access to your Translator resource by assigning the "Cognitive Services User" role to your service principal. Additional information about Microsoft Entra authentication is available here.
Set the values of the client ID, tenant ID, and client secret of the Microsoft Entra application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET. The DefaultAzureCredential constructor uses these variables to create your credentials.
string endpoint = "<Text Translator Custom Endpoint>";
DefaultAzureCredential credential = new DefaultAzureCredential();
TextTranslationClient client = new TextTranslationClient(credential, new Uri(endpoint));
Key concepts
TextTranslationClient
A TextTranslationClient is the primary interface for developers using the Text Translation client library. It provides both synchronous and asynchronous operations to access a specific use of text translator, such as get supported languages detection or text translation.
Input
A TranslateInputItem is a single unit of input to be processed by the translation models in the Translator service. Each TranslateInputItem defines both the input string to translate and the output specifications for the translation. Operations on TextTranslationClient may take a single TranslateInputItem or a collection of TranslateInputItem objects.
For text element length limits, maximum requests size, and supported text encoding see here.
Return value
Return values, such as Response<IReadOnlyList<TranslatedTextItem>>, is the result of a Text Translation operation. It contains an array with one TranslatedTextItem for each input TranslateInputItem. An operation's return value also may optionally include information about the input text element (for example detected language).
Thread safety
We guarantee that all client instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across threads.
Additional concepts
Client options | Accessing the response | Long-running operations | Handling failures | Diagnostics | Mocking | Client lifetime
Examples
The following section provides several code snippets using the client created above, and covers the main features present in this client library. Although the snippets below make use of synchronous service calls, keep in mind that the Azure.AI.Translation.Text package supports both synchronous and asynchronous APIs.
Get Supported Languages
Gets the set of languages currently supported by other operations of the Translator.
try
{
Response<GetSupportedLanguagesResult> response = client.GetSupportedLanguages(cancellationToken: CancellationToken.None);
GetSupportedLanguagesResult languages = response.Value;
Console.WriteLine($"Number of supported languages for translate operations: {languages.Translation.Count}.");
}
catch (RequestFailedException exception)
{
Console.WriteLine($"Error Code: {exception.ErrorCode}");
Console.WriteLine($"Message: {exception.Message}");
}
For samples on using the languages endpoint refer to more samples here.
Please refer to the service documentation for a conceptual discussion of languages.
Translate
The simplest use of the Translate method is to invoke it with a single target language and one input string.
try
{
string targetLanguage = "cs";
string inputText = "This is a test.";
Response<IReadOnlyList<TranslatedTextItem>> response = client.Translate(targetLanguage, inputText);
IReadOnlyList<TranslatedTextItem> translations = response.Value;
TranslatedTextItem translation = translations.FirstOrDefault();
Console.WriteLine($"Detected languages of the input text: {translation?.DetectedLanguage?.Language} with score: {translation?.DetectedLanguage?.Score}.");
Console.WriteLine($"Text was translated to: '{translation?.Translations?.FirstOrDefault().Language}' and the result is: '{translation?.Translations?.FirstOrDefault()?.Text}'.");
}
catch (RequestFailedException exception)
{
Console.WriteLine($"Error Code: {exception.ErrorCode}");
Console.WriteLine($"Message: {exception.Message}");
}
For samples on using the translate endpoint refer to more samples here.
Please refer to the service documentation for a conceptual discussion of translate.
Transliterate
Converts characters or letters of a source language to the corresponding characters or letters of a target language.
try
{
string language = "zh-Hans";
string fromScript = "Hans";
string toScript = "Latn";
string inputText = "这是个测试。";
Response<IReadOnlyList<TransliteratedText>> response = client.Transliterate(language, fromScript, toScript, inputText);
IReadOnlyList<TransliteratedText> transliterations = response.Value;
TransliteratedText transliteration = transliterations.FirstOrDefault();
Console.WriteLine($"Input text was transliterated to '{transliteration?.Script}' script. Transliterated text: '{transliteration?.Text}'.");
}
catch (RequestFailedException exception)
{
Console.WriteLine($"Error Code: {exception.ErrorCode}");
Console.WriteLine($"Message: {exception.Message}");
}
For samples on using the transliterate endpoint refer to more samples here.
Please refer to the service documentation for a conceptual discussion of transliterate.
Troubleshooting
When you interact with the Translator Service using the Text Translation client library, errors returned by the Translator service correspond to the same HTTP status codes returned for REST API requests.
For example, if you submit a translation request without a target translate language, a 400 error is returned, indicating "Bad Request".
try
{
var translation = client.Translate("", "This is a Test");
}
catch (RequestFailedException e)
{
Console.WriteLine(e.ToString());
}
You will notice that additional information is logged, like the client request ID of the operation.
Message:
Azure.RequestFailedException: Service request failed.
Status: 400 (Bad Request)
Content:
{"error":{"code":400036,"message":"The target language is not valid."}}
Headers:
X-RequestId: REDACTED
Access-Control-Expose-Headers: REDACTED
X-Content-Type-Options: REDACTED
Strict-Transport-Security: REDACTED
Date: Mon, 27 Feb 2023 23:31:37 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 71
Setting up console logging
The simplest way to see the logs is to enable the console logging. To create an Azure SDK log listener that outputs messages to console use AzureEventSourceListener.CreateConsoleLogger method.
// Setup a listener to monitor logged events.
using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();
To learn more about other logging mechanisms see here.
Next steps
Samples showing how to use this client library are available in this GitHub repository. Samples are provided for each main functional area, and for each area, samples are provided in both sync and async mode.
Contributing
See the CONTRIBUTING.md for details on building, testing, and contributing to this library.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Azure.Core (>= 1.50.0)
-
net10.0
- Azure.Core (>= 1.50.0)
-
net8.0
- Azure.Core (>= 1.50.0)
NuGet packages (7)
Showing the top 5 NuGet packages that depend on Azure.AI.Translation.Text:
| Package | Downloads |
|---|---|
|
HexaEightGPTMiddleware
Control An AI Assistant like CHATGPT using this Library. Integrate This Library With HexaEight Middleware to create API that produce controlled AI responses |
|
|
BootstrapBlazor.AzureTranslator
Bootstrap UI components extensions of Azure Translator |
|
|
LocalizationProvider.Translator.Azure
Azure Cognitive services implementation for auto-translator feature. |
|
|
DevExpress.AIIntegration.Azure.Translation
Target Platform/Framework: Cross-Platform DevExpress Product Libraries Used: DevExpress AI-powered Extensions for OpenAI, Azure, Ollama, Semantic Kernel (https://www.devexpress.com/ai) Available in the following DevExpress Subscription(s): Universal, DXperience, WinForms, ASP.NET & Blazor (includes DevExtreme), WPF, Reporting, Office File API |
|
|
AzureAI.Community.Microsoft.Semantic.Kernel.Translation
Azure AI Community Microsoft Semantic Kernel Translation Plugin |
GitHub repositories (3)
Showing the top 3 popular GitHub repositories that depend on Azure.AI.Translation.Text:
| Repository | Stars |
|---|---|
|
Richasy/FantasyCopilot
A new-age AI desktop tool
|
|
|
valdisiljuconoks/LocalizationProvider
Database driven localization provider for .NET applications
|
|
|
Richasy/RichasyAssistant
个人助理
|
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.0-beta.1 | 65 | 1/8/2026 |
| 1.0.0 | 817,338 | 5/21/2024 |
| 1.0.0-beta.1 | 234,610 | 4/18/2023 |