diff --git a/src/components/BootstrapBlazor.AzureTranslator/AzureTranslatorOption.cs b/src/components/BootstrapBlazor.AzureTranslator/AzureTranslatorOption.cs
index 58f26d1a8..ece977540 100644
--- a/src/components/BootstrapBlazor.AzureTranslator/AzureTranslatorOption.cs
+++ b/src/components/BootstrapBlazor.AzureTranslator/AzureTranslatorOption.cs
@@ -1,28 +1,32 @@
-// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
+// Copyright (c) Argo Zhang (argo@163.com). 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;
///
-/// AzureTranslatorOption 配置类
+/// Azure 文本翻译配置项。
+/// Azure Text Translation options.
///
public class AzureTranslatorOption
{
///
- /// 获得/设置 订阅 Key 由 Azure 提供
+ /// 获得或设置由 Azure 提供的订阅 Key。
+ /// Gets or sets the subscription Key provided by Azure.
///
[NotNull]
public string? Key { get; set; }
///
- /// 获得/设置 Location/Region 描述 由 Azure 提供
+ /// 获得或设置由 Azure 提供的 Location/Region。
+ /// Gets or sets the Location/Region provided by Azure.
///
[NotNull]
public string? Region { get; set; }
///
- /// 获得/设置 超时时长 默认 0 未设置
+ /// 获得或设置超时时长,默认值为 0,表示未设置。
+ /// Gets or sets the timeout duration. The default is 0, meaning it is not set.
///
public int Timeout { get; set; }
}
diff --git a/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj b/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj
index 9cf410c67..5c3efa427 100644
--- a/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj
+++ b/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj
@@ -1,7 +1,7 @@
- 11.0.1
+ 11.0.2
bb-azuretranslator
@@ -12,8 +12,15 @@
-
+
+
+
+
+
+
+
+
diff --git a/src/components/BootstrapBlazor.AzureTranslator/Extensions/ServiceCollectionExtensions.cs b/src/components/BootstrapBlazor.AzureTranslator/Extensions/ServiceCollectionExtensions.cs
index 5737abdd9..a6f090d29 100644
--- a/src/components/BootstrapBlazor.AzureTranslator/Extensions/ServiceCollectionExtensions.cs
+++ b/src/components/BootstrapBlazor.AzureTranslator/Extensions/ServiceCollectionExtensions.cs
@@ -7,16 +7,18 @@
namespace Microsoft.Extensions.DependencyInjection;
///
-/// BootstrapBlazor 服务扩展类
+/// BootstrapBlazor 服务扩展类。
+/// BootstrapBlazor service extension methods.
///
public static class ServiceCollectionExtensions
{
///
- /// 增加 语音识别服务
+ /// 注册 Azure 文本翻译服务。
+ /// Registers the Azure Text Translation service.
///
- ///
- ///
- ///
+ /// 服务集合。The service collection.
+ /// 可选的配置回调。The optional configuration callback.
+ /// 服务集合。The service collection.
public static IServiceCollection AddBootstrapBlazorAzureTranslator(this IServiceCollection services, Action<
AzureTranslatorOption>? configOptions = null)
{
diff --git a/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs b/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs
index 843bd0196..8bbd7c103 100644
--- a/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs
+++ b/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs
@@ -1,4 +1,4 @@
-// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
+// Copyright (c) Argo Zhang (argo@163.com). 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/
@@ -10,13 +10,15 @@
namespace BootstrapBlazor.Components;
///
-/// AzureTranslatorService 服务实现类
+/// Azure 文本翻译服务实现。
+/// The Azure Text Translation service implementation.
///
///
-/// 构造函数
+/// 使用选项监视器和日志记录器构造服务。
+/// Constructs the service using an options monitor and logger.
///
-///
-///
+/// Azure 文本翻译配置监视器。The Azure Text Translation options monitor.
+/// 日志记录器。The logger.
class AzureTranslatorService(IOptionsMonitor option, ILogger logger) : IAzureTranslatorService
{
private readonly IOptionsMonitor _option = option;
@@ -24,18 +26,18 @@ class AzureTranslatorService(IOptionsMonitor option, ILog
private readonly ILogger _logger = logger;
///
- /// 文本翻译方法
+ /// 翻译文本。
+ /// Translates text.
///
- /// 目标语言
- /// 输入文本
- /// 源语言
- ///
- ///
+ /// 目标语言。The target language.
+ /// 待翻译文本。The text to translate.
+ /// 可选的源语言。The optional source language.
+ /// 取消令牌。The cancellation token.
+ /// 翻译结果列表。A list of translation results.
+ /// 未配置 Azure Translator 的 Key 或 Region 时抛出。Thrown when the Azure Translator Key or Region is not configured.
public async Task> 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? ret = null;
try
{
@@ -50,18 +52,18 @@ public async Task> TranslateAsync(string targe
}
///
- /// 文本翻译方法
+ /// 翻译文本集合。
+ /// Translates a collection of text.
///
- /// 目标语言
- /// 输入文本集合
- /// 源语言
- ///
- ///
+ /// 目标语言。The target language.
+ /// 待翻译文本集合。The collection of text to translate.
+ /// 可选的源语言。The optional source language.
+ /// 取消令牌。The cancellation token.
+ /// 翻译结果列表。A list of translation results.
+ /// 未配置 Azure Translator 的 Key 或 Region 时抛出。Thrown when the Azure Translator Key or Region is not configured.
public async Task> TranslateAsync(string targetLanguage, IEnumerable 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? ret = null;
try
{
@@ -76,22 +78,28 @@ public async Task> TranslateAsync(string targe
}
///
- /// 文本翻译方法
+ /// 翻译文本集合到多个目标语言。
+ /// Translates a collection of text into multiple target languages.
///
- /// 目标语言集合
- /// 输入文本集合
- /// 源语言
- ///
- ///
+ /// 目标语言集合。The collection of target languages.
+ /// 待翻译文本集合。The collection of text to translate.
+ /// 可选的源语言。The optional source language.
+ /// 取消令牌。The cancellation token.
+ /// 翻译结果列表。A list of translation results.
+ /// 未配置 Azure Translator 的 Key 或 Region 时抛出。Thrown when the Azure Translator Key or Region is not configured.
public async Task> TranslateAsync(IEnumerable targetLanguages, IEnumerable 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? 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)
@@ -100,4 +108,25 @@ public async Task> TranslateAsync(IEnumerable<
}
return ret ?? new List();
}
+
+ ///
+ /// 创建 Azure 文本翻译客户端,并校验必需配置。
+ /// Creates an Azure Text Translation client and validates required configuration.
+ ///
+ /// 已配置的文本翻译客户端。The configured Text Translation client.
+ /// 未配置 Azure Translator 的 Key 或 Region 时抛出。Thrown when the Azure Translator Key or Region is not configured.
+ 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);
+ }
}
diff --git a/src/components/BootstrapBlazor.AzureTranslator/Services/IAzureTranslatorService.cs b/src/components/BootstrapBlazor.AzureTranslator/Services/IAzureTranslatorService.cs
index c5a8f86dd..04a443919 100644
--- a/src/components/BootstrapBlazor.AzureTranslator/Services/IAzureTranslatorService.cs
+++ b/src/components/BootstrapBlazor.AzureTranslator/Services/IAzureTranslatorService.cs
@@ -1,4 +1,4 @@
-// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
+// Copyright (c) Argo Zhang (argo@163.com). 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/
@@ -7,37 +7,44 @@
namespace BootstrapBlazor.Components;
///
-/// IAzureTranslatorService 文本翻译服务
+/// Azure 文本翻译服务接口。
+/// The Azure Text Translation service interface.
///
public interface IAzureTranslatorService
{
///
- /// 翻译方法
+ /// 翻译文本。
+ /// Translates text.
///
- ///
- ///
- ///
- ///
- ///
+ /// 目标语言。The target language.
+ /// 待翻译文本。The text to translate.
+ /// 可选的源语言。The optional source language.
+ /// 取消令牌。The cancellation token.
+ /// 翻译结果列表。A list of translation results.
+ /// 未配置 Azure Translator 的 Key 或 Region 时抛出。Thrown when the Azure Translator Key or Region is not configured.
Task> TranslateAsync(string targetLanguage, string inputText, string? sourceLanguage = null, CancellationToken cancellationToken = default);
///
- /// 翻译方法
+ /// 翻译文本集合。
+ /// Translates a collection of text.
///
- ///
- ///
- ///
- ///
- ///
+ /// 目标语言。The target language.
+ /// 待翻译文本集合。The collection of text to translate.
+ /// 可选的源语言。The optional source language.
+ /// 取消令牌。The cancellation token.
+ /// 翻译结果列表。A list of translation results.
+ /// 未配置 Azure Translator 的 Key 或 Region 时抛出。Thrown when the Azure Translator Key or Region is not configured.
Task> TranslateAsync(string targetLanguage, IEnumerable content, string? sourceLanguage = null, CancellationToken cancellationToken = default);
///
- /// 翻译方法
+ /// 翻译文本集合到多个目标语言。
+ /// Translates a collection of text into multiple target languages.
///
- ///
- ///
- ///
- ///
- ///
+ /// 目标语言集合。The collection of target languages.
+ /// 待翻译文本集合。The collection of text to translate.
+ /// 可选的源语言。The optional source language.
+ /// 取消令牌。The cancellation token.
+ /// 翻译结果列表。A list of translation results.
+ /// 未配置 Azure Translator 的 Key 或 Region 时抛出。Thrown when the Azure Translator Key or Region is not configured.
Task> TranslateAsync(IEnumerable targetLanguages, IEnumerable content, string? sourceLanguage = null, CancellationToken cancellationToken = default);
}