Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

namespace BootstrapBlazor.Components;

/// <summary>
/// AzureTranslatorOption 配置类
/// <para lang="zh">Azure 文本翻译配置项。</para>
/// <para lang="en">Azure Text Translation options.</para>
/// </summary>
public class AzureTranslatorOption
{
/// <summary>
/// 获得/设置 订阅 Key 由 Azure 提供
/// <para lang="zh">获得或设置由 Azure 提供的订阅 Key。</para>
/// <para lang="en">Gets or sets the subscription Key provided by Azure.</para>
/// </summary>
[NotNull]
public string? Key { get; set; }

/// <summary>
/// 获得/设置 Location/Region 描述 由 Azure 提供
/// <para lang="zh">获得或设置由 Azure 提供的 Location/Region。</para>
/// <para lang="en">Gets or sets the Location/Region provided by Azure.</para>
/// </summary>
[NotNull]
public string? Region { get; set; }

/// <summary>
/// 获得/设置 超时时长 默认 0 未设置
/// <para lang="zh">获得或设置超时时长,默认值为 0,表示未设置。</para>
/// <para lang="en">Gets or sets the timeout duration. The default is 0, meaning it is not set.</para>
/// </summary>
public int Timeout { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>11.0.1</Version>
<Version>11.0.2</Version>
<SkillName>bb-azuretranslator</SkillName>
</PropertyGroup>

Expand All @@ -12,8 +12,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.Translation.Text" Version="1.0.0" />
<PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0' Or '$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Azure.AI.Translation.Text" Version="1.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'net6.0' And '$(TargetFramework)' != 'net7.0'">
<PackageReference Include="Azure.AI.Translation.Text" Version="2.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// BootstrapBlazor 服务扩展类
/// <para lang="zh">BootstrapBlazor 服务扩展类。</para>
/// <para lang="en">BootstrapBlazor service extension methods.</para>
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// 增加 语音识别服务
/// <para lang="zh">注册 Azure 文本翻译服务。</para>
/// <para lang="en">Registers the Azure Text Translation service.</para>
/// </summary>
/// <param name="services"></param>
/// <param name="configOptions"></param>
/// <returns></returns>
/// <param name="services"><para lang="zh">服务集合。</para><para lang="en">The service collection.</para></param>
/// <param name="configOptions"><para lang="zh">可选的配置回调。</para><para lang="en">The optional configuration callback.</para></param>
/// <returns><para lang="zh">服务集合。</para><para lang="en">The service collection.</para></returns>
public static IServiceCollection AddBootstrapBlazorAzureTranslator(this IServiceCollection services, Action<
AzureTranslatorOption>? configOptions = null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

Expand All @@ -10,32 +10,34 @@
namespace BootstrapBlazor.Components;

/// <summary>
/// AzureTranslatorService 服务实现类
/// <para lang="zh">Azure 文本翻译服务实现。</para>
/// <para lang="en">The Azure Text Translation service implementation.</para>
/// </summary>
/// <remarks>
/// 构造函数
/// <para lang="zh">使用选项监视器和日志记录器构造服务。</para>
/// <para lang="en">Constructs the service using an options monitor and logger.</para>
/// </remarks>
/// <param name="option"></param>
/// <param name="logger"></param>
/// <param name="option"><para lang="zh">Azure 文本翻译配置监视器。</para><para lang="en">The Azure Text Translation options monitor.</para></param>
/// <param name="logger"><para lang="zh">日志记录器。</para><para lang="en">The logger.</para></param>
class AzureTranslatorService(IOptionsMonitor<AzureTranslatorOption> option, ILogger<AzureTranslatorService> logger) : IAzureTranslatorService
{
private readonly IOptionsMonitor<AzureTranslatorOption> _option = option;

private readonly ILogger<AzureTranslatorService> _logger = logger;

/// <summary>
/// 文本翻译方法
/// <para lang="zh">翻译文本。</para>
/// <para lang="en">Translates text.</para>
/// </summary>
/// <param name="targetLanguage">目标语言</param>
/// <param name="inputText">输入文本</param>
/// <param name="sourceLanguage">源语言</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <param name="targetLanguage"><para lang="zh">目标语言。</para><para lang="en">The target language.</para></param>
/// <param name="inputText"><para lang="zh">待翻译文本。</para><para lang="en">The text to translate.</para></param>
/// <param name="sourceLanguage"><para lang="zh">可选的源语言。</para><para lang="en">The optional source language.</para></param>
/// <param name="cancellationToken"><para lang="zh">取消令牌。</para><para lang="en">The cancellation token.</para></param>
/// <returns><para lang="zh">翻译结果列表。</para><para lang="en">A list of translation results.</para></returns>
/// <exception cref="InvalidOperationException"><para lang="zh">未配置 Azure Translator 的 Key 或 Region 时抛出。</para><para lang="en">Thrown when the Azure Translator Key or Region is not configured.</para></exception>
public async Task<IReadOnlyList<TranslatedTextItem>> TranslateAsync(string targetLanguage, string inputText, string? sourceLanguage = null, CancellationToken cancellationToken = default)
{
var key = _option.CurrentValue.Key;
var region = _option.CurrentValue.Region;
var client = new TextTranslationClient(new AzureKeyCredential(key), region);
var client = CreateClient();
IReadOnlyList<TranslatedTextItem>? ret = null;
try
{
Expand All @@ -50,18 +52,18 @@ public async Task<IReadOnlyList<TranslatedTextItem>> TranslateAsync(string targe
}

/// <summary>
/// 文本翻译方法
/// <para lang="zh">翻译文本集合。</para>
/// <para lang="en">Translates a collection of text.</para>
/// </summary>
/// <param name="targetLanguage">目标语言</param>
/// <param name="content">输入文本集合</param>
/// <param name="sourceLanguage">源语言</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <param name="targetLanguage"><para lang="zh">目标语言。</para><para lang="en">The target language.</para></param>
/// <param name="content"><para lang="zh">待翻译文本集合。</para><para lang="en">The collection of text to translate.</para></param>
/// <param name="sourceLanguage"><para lang="zh">可选的源语言。</para><para lang="en">The optional source language.</para></param>
/// <param name="cancellationToken"><para lang="zh">取消令牌。</para><para lang="en">The cancellation token.</para></param>
/// <returns><para lang="zh">翻译结果列表。</para><para lang="en">A list of translation results.</para></returns>
/// <exception cref="InvalidOperationException"><para lang="zh">未配置 Azure Translator 的 Key 或 Region 时抛出。</para><para lang="en">Thrown when the Azure Translator Key or Region is not configured.</para></exception>
public async Task<IReadOnlyList<TranslatedTextItem>> TranslateAsync(string targetLanguage, IEnumerable<string> content, string? sourceLanguage = null, CancellationToken cancellationToken = default)
{
var key = _option.CurrentValue.Key;
var region = _option.CurrentValue.Region;
var client = new TextTranslationClient(new AzureKeyCredential(key), region);
var client = CreateClient();
IReadOnlyList<TranslatedTextItem>? ret = null;
try
{
Expand All @@ -76,22 +78,28 @@ public async Task<IReadOnlyList<TranslatedTextItem>> TranslateAsync(string targe
}

/// <summary>
/// 文本翻译方法
/// <para lang="zh">翻译文本集合到多个目标语言。</para>
/// <para lang="en">Translates a collection of text into multiple target languages.</para>
/// </summary>
/// <param name="targetLanguages">目标语言集合</param>
/// <param name="content">输入文本集合</param>
/// <param name="sourceLanguage">源语言</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <param name="targetLanguages"><para lang="zh">目标语言集合。</para><para lang="en">The collection of target languages.</para></param>
/// <param name="content"><para lang="zh">待翻译文本集合。</para><para lang="en">The collection of text to translate.</para></param>
/// <param name="sourceLanguage"><para lang="zh">可选的源语言。</para><para lang="en">The optional source language.</para></param>
/// <param name="cancellationToken"><para lang="zh">取消令牌。</para><para lang="en">The cancellation token.</para></param>
/// <returns><para lang="zh">翻译结果列表。</para><para lang="en">A list of translation results.</para></returns>
/// <exception cref="InvalidOperationException"><para lang="zh">未配置 Azure Translator 的 Key 或 Region 时抛出。</para><para lang="en">Thrown when the Azure Translator Key or Region is not configured.</para></exception>
public async Task<IReadOnlyList<TranslatedTextItem>> TranslateAsync(IEnumerable<string> targetLanguages, IEnumerable<string> content, string? sourceLanguage = null, CancellationToken cancellationToken = default)
{
var key = _option.CurrentValue.Key;
var region = _option.CurrentValue.Region;
var client = new TextTranslationClient(new AzureKeyCredential(key), region);
var client = CreateClient();
IReadOnlyList<TranslatedTextItem>? ret = null;
try
{
#if NET6_0 || NET7_0
var response = await client.TranslateAsync(targetLanguages, content, sourceLanguage: sourceLanguage, cancellationToken: cancellationToken);
#else
var targets = targetLanguages.Select(language => new TranslationTarget(language)).ToArray();
var inputs = content.Select(text => new TranslateInputItem(text, targets, language: sourceLanguage));
var response = await client.TranslateAsync(inputs, cancellationToken);
#endif
ret = response.Value;
}
catch (RequestFailedException ex)
Expand All @@ -100,4 +108,25 @@ public async Task<IReadOnlyList<TranslatedTextItem>> TranslateAsync(IEnumerable<
}
return ret ?? new List<TranslatedTextItem>();
}

/// <summary>
/// <para lang="zh">创建 Azure 文本翻译客户端,并校验必需配置。</para>
/// <para lang="en">Creates an Azure Text Translation client and validates required configuration.</para>
/// </summary>
/// <returns><para lang="zh">已配置的文本翻译客户端。</para><para lang="en">The configured Text Translation client.</para></returns>
/// <exception cref="InvalidOperationException"><para lang="zh">未配置 Azure Translator 的 Key 或 Region 时抛出。</para><para lang="en">Thrown when the Azure Translator Key or Region is not configured.</para></exception>
private TextTranslationClient CreateClient()
{
var option = _option.CurrentValue;
if (string.IsNullOrWhiteSpace(option.Key))
{
throw new InvalidOperationException("Azure Translator Key is not configured.");
}
if (string.IsNullOrWhiteSpace(option.Region))
{
throw new InvalidOperationException("Azure Translator Location/Region is not configured.");
}

return new TextTranslationClient(new AzureKeyCredential(option.Key), option.Region);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

Expand All @@ -7,37 +7,44 @@
namespace BootstrapBlazor.Components;

/// <summary>
/// IAzureTranslatorService 文本翻译服务
/// <para lang="zh">Azure 文本翻译服务接口。</para>
/// <para lang="en">The Azure Text Translation service interface.</para>
/// </summary>
public interface IAzureTranslatorService
{
/// <summary>
/// 翻译方法
/// <para lang="zh">翻译文本。</para>
/// <para lang="en">Translates text.</para>
/// </summary>
/// <param name="targetLanguage"></param>
/// <param name="inputText"></param>
/// <param name="sourceLanguage"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <param name="targetLanguage"><para lang="zh">目标语言。</para><para lang="en">The target language.</para></param>
/// <param name="inputText"><para lang="zh">待翻译文本。</para><para lang="en">The text to translate.</para></param>
/// <param name="sourceLanguage"><para lang="zh">可选的源语言。</para><para lang="en">The optional source language.</para></param>
/// <param name="cancellationToken"><para lang="zh">取消令牌。</para><para lang="en">The cancellation token.</para></param>
/// <returns><para lang="zh">翻译结果列表。</para><para lang="en">A list of translation results.</para></returns>
/// <exception cref="InvalidOperationException"><para lang="zh">未配置 Azure Translator 的 Key 或 Region 时抛出。</para><para lang="en">Thrown when the Azure Translator Key or Region is not configured.</para></exception>
Task<IReadOnlyList<TranslatedTextItem>> TranslateAsync(string targetLanguage, string inputText, string? sourceLanguage = null, CancellationToken cancellationToken = default);

/// <summary>
/// 翻译方法
/// <para lang="zh">翻译文本集合。</para>
/// <para lang="en">Translates a collection of text.</para>
/// </summary>
/// <param name="targetLanguage"></param>
/// <param name="content"></param>
/// <param name="sourceLanguage"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <param name="targetLanguage"><para lang="zh">目标语言。</para><para lang="en">The target language.</para></param>
/// <param name="content"><para lang="zh">待翻译文本集合。</para><para lang="en">The collection of text to translate.</para></param>
/// <param name="sourceLanguage"><para lang="zh">可选的源语言。</para><para lang="en">The optional source language.</para></param>
/// <param name="cancellationToken"><para lang="zh">取消令牌。</para><para lang="en">The cancellation token.</para></param>
/// <returns><para lang="zh">翻译结果列表。</para><para lang="en">A list of translation results.</para></returns>
/// <exception cref="InvalidOperationException"><para lang="zh">未配置 Azure Translator 的 Key 或 Region 时抛出。</para><para lang="en">Thrown when the Azure Translator Key or Region is not configured.</para></exception>
Task<IReadOnlyList<TranslatedTextItem>> TranslateAsync(string targetLanguage, IEnumerable<string> content, string? sourceLanguage = null, CancellationToken cancellationToken = default);

/// <summary>
/// 翻译方法
/// <para lang="zh">翻译文本集合到多个目标语言。</para>
/// <para lang="en">Translates a collection of text into multiple target languages.</para>
/// </summary>
/// <param name="targetLanguages"></param>
/// <param name="content"></param>
/// <param name="sourceLanguage"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <param name="targetLanguages"><para lang="zh">目标语言集合。</para><para lang="en">The collection of target languages.</para></param>
/// <param name="content"><para lang="zh">待翻译文本集合。</para><para lang="en">The collection of text to translate.</para></param>
/// <param name="sourceLanguage"><para lang="zh">可选的源语言。</para><para lang="en">The optional source language.</para></param>
/// <param name="cancellationToken"><para lang="zh">取消令牌。</para><para lang="en">The cancellation token.</para></param>
/// <returns><para lang="zh">翻译结果列表。</para><para lang="en">A list of translation results.</para></returns>
/// <exception cref="InvalidOperationException"><para lang="zh">未配置 Azure Translator 的 Key 或 Region 时抛出。</para><para lang="en">Thrown when the Azure Translator Key or Region is not configured.</para></exception>
Task<IReadOnlyList<TranslatedTextItem>> TranslateAsync(IEnumerable<string> targetLanguages, IEnumerable<string> content, string? sourceLanguage = null, CancellationToken cancellationToken = default);
}
Loading