From 13846c084e2bfd9c5512a2caf3de788da39f32e2 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sat, 5 Dec 2026 12:58:24 +0800 Subject: [PATCH 1/6] chore: bump version 11.0.2 --- .../BootstrapBlazor.AzureTranslator.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj b/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj index 9cf410c6..aeac5a6c 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 From d40ab7f3b1a741ea91c0895ded175baca9c17319 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sat, 5 Dec 2026 12:58:36 +0800 Subject: [PATCH 2/6] =?UTF-8?q?chore:=20=E4=BE=9D=E8=B5=96=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=88=B0=202.0.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BootstrapBlazor.AzureTranslator.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj b/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj index aeac5a6c..72c78ead 100644 --- a/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj +++ b/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj @@ -12,7 +12,7 @@ - + From f9e1bc0cb1127cdd8e1b5ea7abbcdde7ed7facef Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sat, 5 Dec 2026 12:59:01 +0800 Subject: [PATCH 3/6] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=A4=8D=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/AzureTranslatorService.cs | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs b/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs index 843bd019..78a73b03 100644 --- a/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs +++ b/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs @@ -33,9 +33,7 @@ class AzureTranslatorService(IOptionsMonitor option, ILog /// 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 { @@ -59,9 +57,7 @@ public async Task> TranslateAsync(string targe /// 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 { @@ -85,9 +81,7 @@ public async Task> TranslateAsync(string targe /// 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 { @@ -100,4 +94,19 @@ public async Task> TranslateAsync(IEnumerable< } return ret ?? new List(); } + + 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); + } } From 1c8b8da8aef326b943a0c3d077f7a05cfda4a83f Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sat, 5 Dec 2026 12:59:32 +0800 Subject: [PATCH 4/6] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=20api=20?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/AzureTranslatorService.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs b/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs index 78a73b03..f7f39f17 100644 --- a/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs +++ b/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs @@ -85,7 +85,9 @@ public async Task> TranslateAsync(IEnumerable< IReadOnlyList? ret = null; try { - var response = await client.TranslateAsync(targetLanguages, content, sourceLanguage: sourceLanguage, cancellationToken: cancellationToken); + 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); ret = response.Value; } catch (RequestFailedException ex) From 287783ec51debdf0e5d044849ba2b0b7b7df8417 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sat, 5 Dec 2026 13:30:36 +0800 Subject: [PATCH 5/6] =?UTF-8?q?refactor:=20=E9=92=88=E5=AF=B9=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=E8=B0=83=E7=94=A8=E4=B8=8D=E5=90=8C=E7=9A=84=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BootstrapBlazor.AzureTranslator.csproj | 9 ++++++++- .../Services/AzureTranslatorService.cs | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj b/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj index 72c78ead..5c3efa42 100644 --- a/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj +++ b/src/components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj @@ -12,8 +12,15 @@ - + + + + + + + + diff --git a/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs b/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs index f7f39f17..6a458da5 100644 --- a/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs +++ b/src/components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs @@ -85,9 +85,13 @@ public async Task> TranslateAsync(IEnumerable< 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) From 5d553b4e127b0ab23df00ef7f8872e694ad98121 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sat, 5 Dec 2026 13:47:48 +0800 Subject: [PATCH 6/6] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AzureTranslatorOption.cs | 14 +++-- .../Extensions/ServiceCollectionExtensions.cs | 12 ++-- .../Services/AzureTranslatorService.cs | 60 ++++++++++++------- .../Services/IAzureTranslatorService.cs | 47 ++++++++------- 4 files changed, 80 insertions(+), 53 deletions(-) diff --git a/src/components/BootstrapBlazor.AzureTranslator/AzureTranslatorOption.cs b/src/components/BootstrapBlazor.AzureTranslator/AzureTranslatorOption.cs index 58f26d1a..ece97754 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/Extensions/ServiceCollectionExtensions.cs b/src/components/BootstrapBlazor.AzureTranslator/Extensions/ServiceCollectionExtensions.cs index 5737abdd..a6f090d2 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 6a458da5..8bbd7c10 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,13 +26,15 @@ 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 client = CreateClient(); @@ -48,13 +52,15 @@ 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 client = CreateClient(); @@ -72,13 +78,15 @@ 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 client = CreateClient(); @@ -101,6 +109,12 @@ 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; diff --git a/src/components/BootstrapBlazor.AzureTranslator/Services/IAzureTranslatorService.cs b/src/components/BootstrapBlazor.AzureTranslator/Services/IAzureTranslatorService.cs index c5a8f86d..04a44391 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); }