Encamina.Enmarcha.AI.TextsTranslation.Azure
8.1.2-preview-12
See the version list below for details.
dotnet add package Encamina.Enmarcha.AI.TextsTranslation.Azure --version 8.1.2-preview-12
NuGet\Install-Package Encamina.Enmarcha.AI.TextsTranslation.Azure -Version 8.1.2-preview-12
<PackageReference Include="Encamina.Enmarcha.AI.TextsTranslation.Azure" Version="8.1.2-preview-12" />
paket add Encamina.Enmarcha.AI.TextsTranslation.Azure --version 8.1.2-preview-12
#r "nuget: Encamina.Enmarcha.AI.TextsTranslation.Azure, 8.1.2-preview-12"
// Install Encamina.Enmarcha.AI.TextsTranslation.Azure as a Cake Addin #addin nuget:?package=Encamina.Enmarcha.AI.TextsTranslation.Azure&version=8.1.2-preview-12&prerelease // Install Encamina.Enmarcha.AI.TextsTranslation.Azure as a Cake Tool #tool nuget:?package=Encamina.Enmarcha.AI.TextsTranslation.Azure&version=8.1.2-preview-12&prerelease
AI - Azure Texts Translation
Azure Texts Translation is a wrapper project for Azure AI Translator API REST. Its main functionality is to simplify and abstract the usage of API REST, primarily focused on text translations.
Setup
Nuget package
First, install NuGet. Then, install Encamina.Enmarcha.AI.TextsTranslation.Azure from the package manager console:
PM> Install-Package Encamina.Enmarcha.AI.TextsTranslation.Azure
.NET CLI:
First, install .NET CLI. Then, install Encamina.Enmarcha.AI.TextsTranslation.Azure from the .NET CLI:
dotnet add package Encamina.Enmarcha.AI.TextsTranslation.Azure
How to use
Question Answering
First, you need to add the TextTranslationConfigurations to your project configuration. You can achieve this by using any configuration provider. The followng code is an example of how the settings would appear using the appsettings.json file:
{
// ...
"TextTranslationConfigurations": {
"TextTranslationOptions": [
{
"Name": "DefaultTextTranslation", // Name of this configuration
"EndpointUrl": "https://example.cognitiveservices.azure.com/", // Azure AI Translator endpoint's url
"KeyCredential": "<API-KEY>", // Azure AI Translator (security) key
"RegionName": "westeurope" // Azure region name of the translator resource.
}
]
}
// ...
}
Next, in Program.cs
or a similar entry point file in your project, add the following code.
// Entry point
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
// ...
});
// ...
// Or others configuration providers...
builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
builder.Services.AddDefaultCognitiveServiceProvider()
.AddAzureTextTranslationServices(builder.Configuration);
The extension methods AddDefaultCognitiveServiceProvider
and AddAzureTextTranslationServices
manage the configuration to create instances of ICognitiveServiceProvider
. With this, you can retrieve instances of ITextTranslationService
(whose implementation is TextTranslationService). As seen in the configuration JSON, TextTranslationConfigurations
is an array, allowing you to generate different Text Translations configurations and retrieve the appropriate one based on the Name
. Now, you can inject ICognitiveServiceProvider
through the constructor for use.
public class MyClass
{
private readonly ITextTranslationService textTranslationService;
public MyClass(ICognitiveServiceProvider cognitiveServiceProvider)
{
// The value "DefaultTextTranslation" is the name specified in the JSON from the previous code.
// This is just an example code; avoid hardcoding strings :)
textTranslationService = cognitiveServiceProvider.GetTextTranslationService("DefaultTextTranslation");
}
public async Task<string> GetEnglishTranslationAsync(string text, CancellationToken cancellationToken)
{
const string languageName = "en-ES";
const string key = "text_key";
var textTranslationRequest = new TextTranslationRequest()
{
ToLanguages = new CultureInfo[] { new CultureInfo(languageName) },
Texts = new Dictionary<string, string>() { { key, text } },
};
var textTranslationResult = await textTranslationService.TranslateAsync(textTranslationRequest, cancellationToken);
var translations = textTranslationResult.TextTranslations?.FirstOrDefault(t => t.Id == key)?.Translations;
return translations != null && translations.TryGetValue(languageName, out var englishTranslation)
? englishTranslation
: null;
}
}
In addition, you can intercept the translated texts to apply fixes or changes to overcome or correct any discrepancy. To do this, simply implement the ITextTranslationNormalizer
interface.
public class NumberNormalizer : ITextTranslationNormalizer
{
private static readonly Dictionary<string, string> numbers = new Dictionary<string, string>
{
{"0", "Zero"},
{"1", "One"},
{"2", "Two"},
{"3", "Three"},
{"4", "Four"},
{"5", "Five"},
{"6", "Six"},
{"7", "Seven"},
{"8", "Eight"},
{"9", "Nine"}
};
public int Order => int.MinValue;
public string Normalize(string value)
{
foreach (var number in numbers)
{
value = value.Replace(number.Key, number.Value, StringComparison.InvariantCulture);
}
return value;
}
}
public class TrimNormalizer : ITextTranslationNormalizer
{
public int Order => int.MaxValue;
public string Normalize(string value)
{
return value.Trim();
}
}
NumberNormalizer
replaces any occurrence of a number (1, 2, 3...) with its textual representation (one, two, three...), and TrimNormalizer
simply removes leading and trailing spaces from the text. As you can see, the value of the Order
property indicates that NumberNormalizer
will be applied first, followed by TrimNormalizer
. In the initial code, it is sufficient to add the created normalizers.
// Entry point
// ...
builder.Services.AddDefaultCognitiveServiceProvider()
.AddAzureTextTranslationServices(builder.Configuration)
.UseNormalizer<NumberNormalizer>()
.UseNormalizer<TrimNormalizer>();
Now, when you execute the code for the previous translation (the MyClass
class), it will automatically utilize the normalizers on the output text.
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 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | 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.1
- CommunityToolkit.Diagnostics (>= 8.2.2)
- Encamina.Enmarcha.AI (>= 8.1.2-preview-12)
- Encamina.Enmarcha.AI.TextsTranslation.Abstractions (>= 8.1.2-preview-12)
- Encamina.Enmarcha.Core (>= 8.1.2-preview-12)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Hosting (>= 8.0.0)
- Microsoft.Extensions.Http (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- Microsoft.Extensions.Options.DataAnnotations (>= 8.0.0)
- System.Net.Http.Json (>= 8.0.0)
- System.Text.Json (>= 8.0.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 |
---|---|---|
8.2.0 | 70 | 10/22/2024 |
8.2.0-preview-01-m01 | 86 | 9/17/2024 |
8.1.9-preview-02 | 59 | 10/22/2024 |
8.1.9-preview-01 | 70 | 10/4/2024 |
8.1.8 | 80 | 9/23/2024 |
8.1.8-preview-07 | 68 | 9/12/2024 |
8.1.8-preview-06 | 83 | 9/11/2024 |
8.1.8-preview-05 | 79 | 9/10/2024 |
8.1.8-preview-04 | 97 | 8/16/2024 |
8.1.8-preview-03 | 92 | 8/13/2024 |
8.1.8-preview-02 | 86 | 8/13/2024 |
8.1.8-preview-01 | 77 | 8/12/2024 |
8.1.7 | 86 | 8/7/2024 |
8.1.7-preview-09 | 98 | 7/3/2024 |
8.1.7-preview-08 | 83 | 7/2/2024 |
8.1.7-preview-07 | 77 | 6/10/2024 |
8.1.7-preview-06 | 64 | 6/10/2024 |
8.1.7-preview-05 | 84 | 6/6/2024 |
8.1.7-preview-04 | 79 | 6/6/2024 |
8.1.7-preview-03 | 107 | 5/24/2024 |
8.1.7-preview-02 | 91 | 5/10/2024 |
8.1.7-preview-01 | 89 | 5/8/2024 |
8.1.6 | 100 | 5/7/2024 |
8.1.6-preview-08 | 48 | 5/2/2024 |
8.1.6-preview-07 | 87 | 4/29/2024 |
8.1.6-preview-06 | 95 | 4/26/2024 |
8.1.6-preview-05 | 96 | 4/24/2024 |
8.1.6-preview-04 | 79 | 4/22/2024 |
8.1.6-preview-03 | 86 | 4/22/2024 |
8.1.6-preview-02 | 89 | 4/17/2024 |
8.1.6-preview-01 | 90 | 4/15/2024 |
8.1.5 | 108 | 4/15/2024 |
8.1.5-preview-15 | 80 | 4/10/2024 |
8.1.5-preview-14 | 89 | 3/20/2024 |
8.1.5-preview-13 | 80 | 3/18/2024 |
8.1.5-preview-12 | 106 | 3/13/2024 |
8.1.5-preview-11 | 77 | 3/13/2024 |
8.1.5-preview-10 | 89 | 3/13/2024 |
8.1.5-preview-09 | 74 | 3/12/2024 |
8.1.5-preview-08 | 76 | 3/12/2024 |
8.1.5-preview-07 | 90 | 3/8/2024 |
8.1.5-preview-06 | 64 | 3/8/2024 |
8.1.5-preview-05 | 87 | 3/7/2024 |
8.1.5-preview-04 | 89 | 3/7/2024 |
8.1.5-preview-03 | 86 | 3/7/2024 |
8.1.5-preview-02 | 98 | 2/28/2024 |
8.1.5-preview-01 | 75 | 2/19/2024 |
8.1.4 | 129 | 2/15/2024 |
8.1.3 | 115 | 2/13/2024 |
8.1.3-preview-07 | 74 | 2/13/2024 |
8.1.3-preview-06 | 86 | 2/12/2024 |
8.1.3-preview-05 | 84 | 2/9/2024 |
8.1.3-preview-04 | 85 | 2/8/2024 |
8.1.3-preview-03 | 90 | 2/7/2024 |
8.1.3-preview-02 | 69 | 2/2/2024 |
8.1.3-preview-01 | 77 | 2/2/2024 |
8.1.2 | 109 | 2/1/2024 |
8.1.2-preview-9 | 92 | 1/22/2024 |
8.1.2-preview-8 | 66 | 1/19/2024 |
8.1.2-preview-7 | 89 | 1/19/2024 |
8.1.2-preview-6 | 85 | 1/19/2024 |
8.1.2-preview-5 | 78 | 1/19/2024 |
8.1.2-preview-4 | 79 | 1/19/2024 |
8.1.2-preview-3 | 69 | 1/18/2024 |
8.1.2-preview-2 | 80 | 1/18/2024 |
8.1.2-preview-16 | 78 | 1/31/2024 |
8.1.2-preview-15 | 74 | 1/31/2024 |
8.1.2-preview-14 | 85 | 1/25/2024 |
8.1.2-preview-13 | 73 | 1/25/2024 |
8.1.2-preview-12 | 78 | 1/23/2024 |
8.1.2-preview-11 | 92 | 1/23/2024 |
8.1.2-preview-10 | 81 | 1/22/2024 |
8.1.2-preview-1 | 76 | 1/18/2024 |
8.1.1 | 109 | 1/18/2024 |
8.1.0 | 102 | 1/18/2024 |
8.0.3 | 149 | 12/29/2023 |
8.0.1 | 120 | 12/14/2023 |
8.0.0 | 124 | 12/7/2023 |
6.0.4.3 | 119 | 12/29/2023 |
6.0.4.2 | 85 | 12/20/2023 |
6.0.4.1 | 87 | 12/19/2023 |
6.0.4 | 119 | 12/4/2023 |
6.0.3.20 | 108 | 11/27/2023 |
6.0.3.19 | 99 | 11/22/2023 |