From f88b38a5becfbf0f54722adabe2ed09ba6d25ce2 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Sat, 9 Dec 2023 12:38:49 +0800 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20Translator=20?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BootstrapBlazor.sln | 7 ++ .../AzureTranslatorOption.cs | 28 +++++ .../BootstrapBlazor.AzureTranslator.csproj | 18 +++ .../Extensions/ServiceCollectionExtensions.cs | 35 ++++++ .../Services/AzureTranslatorService.cs | 103 ++++++++++++++++++ .../Services/IAzureTranslatorService.cs | 43 ++++++++ .../BootstrapBlazor.AzureTranslator/logo.png | Bin 0 -> 6414 bytes 7 files changed, 234 insertions(+) create mode 100644 src/Extensions/Components/BootstrapBlazor.AzureTranslator/AzureTranslatorOption.cs create mode 100644 src/Extensions/Components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj create mode 100644 src/Extensions/Components/BootstrapBlazor.AzureTranslator/Extensions/ServiceCollectionExtensions.cs create mode 100644 src/Extensions/Components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs create mode 100644 src/Extensions/Components/BootstrapBlazor.AzureTranslator/Services/IAzureTranslatorService.cs create mode 100644 src/Extensions/Components/BootstrapBlazor.AzureTranslator/logo.png diff --git a/BootstrapBlazor.sln b/BootstrapBlazor.sln index 3e737d0e1e8..0391a223573 100644 --- a/BootstrapBlazor.sln +++ b/BootstrapBlazor.sln @@ -120,6 +120,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "configuration", "configurat exclusion.dic = exclusion.dic EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapBlazor.AzureTranslator", "src\Extensions\Components\BootstrapBlazor.AzureTranslator\BootstrapBlazor.AzureTranslator.csproj", "{D63E50CB-EF78-47BF-9809-003D4574ABF4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -258,6 +260,10 @@ Global {49D9E679-6824-43DF-9DAA-70E24ACCAA62}.Debug|Any CPU.Build.0 = Debug|Any CPU {49D9E679-6824-43DF-9DAA-70E24ACCAA62}.Release|Any CPU.ActiveCfg = Release|Any CPU {49D9E679-6824-43DF-9DAA-70E24ACCAA62}.Release|Any CPU.Build.0 = Release|Any CPU + {D63E50CB-EF78-47BF-9809-003D4574ABF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D63E50CB-EF78-47BF-9809-003D4574ABF4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D63E50CB-EF78-47BF-9809-003D4574ABF4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D63E50CB-EF78-47BF-9809-003D4574ABF4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -303,6 +309,7 @@ Global {568237B9-209E-49B1-9BCF-E853F2D61442} = {CD062AB6-244D-402A-8F33-C37DAC5856CC} {25B67FF0-3D70-47D8-AC70-1934442A8881} = {CD062AB6-244D-402A-8F33-C37DAC5856CC} {49D9E679-6824-43DF-9DAA-70E24ACCAA62} = {CD062AB6-244D-402A-8F33-C37DAC5856CC} + {D63E50CB-EF78-47BF-9809-003D4574ABF4} = {CD062AB6-244D-402A-8F33-C37DAC5856CC} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {0DCB0756-34FA-4FD0-AE1D-D3F08B5B3A6B} diff --git a/src/Extensions/Components/BootstrapBlazor.AzureTranslator/AzureTranslatorOption.cs b/src/Extensions/Components/BootstrapBlazor.AzureTranslator/AzureTranslatorOption.cs new file mode 100644 index 00000000000..58f26d1a84e --- /dev/null +++ b/src/Extensions/Components/BootstrapBlazor.AzureTranslator/AzureTranslatorOption.cs @@ -0,0 +1,28 @@ +// 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 配置类 +/// +public class AzureTranslatorOption +{ + /// + /// 获得/设置 订阅 Key 由 Azure 提供 + /// + [NotNull] + public string? Key { get; set; } + + /// + /// 获得/设置 Location/Region 描述 由 Azure 提供 + /// + [NotNull] + public string? Region { get; set; } + + /// + /// 获得/设置 超时时长 默认 0 未设置 + /// + public int Timeout { get; set; } +} diff --git a/src/Extensions/Components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj b/src/Extensions/Components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj new file mode 100644 index 00000000000..2050693171a --- /dev/null +++ b/src/Extensions/Components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj @@ -0,0 +1,18 @@ + + + + 8.0.0 + + + + Bootstrap Blazor Azure WebAssembly wasm Translator Components + Bootstrap UI components extensions of Azure Translator + BootstrapBlazor.Components + + + + + + + + diff --git a/src/Extensions/Components/BootstrapBlazor.AzureTranslator/Extensions/ServiceCollectionExtensions.cs b/src/Extensions/Components/BootstrapBlazor.AzureTranslator/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 00000000000..ba39c3a3436 --- /dev/null +++ b/src/Extensions/Components/BootstrapBlazor.AzureTranslator/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,35 @@ +// 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/ + +using BootstrapBlazor.Components; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace Microsoft.Extensions.DependencyInjection; + +/// +/// BootstrapBlazor 服务扩展类 +/// +public static class ServiceCollectionExtensions +{ + /// + /// 增加 语音识别服务 + /// + /// + /// + /// + public static IServiceCollection AddBootstrapBlazorAzureTranslator(this IServiceCollection services, Action< + AzureTranslatorOption>? configOptions = null) + { + services.AddHttpClient(); + + services.AddSingleton(); + services.AddOptionsMonitor(); + + services.Configure(option => + { + configOptions?.Invoke(option); + }); + return services; + } +} diff --git a/src/Extensions/Components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs b/src/Extensions/Components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs new file mode 100644 index 00000000000..843bd0196fd --- /dev/null +++ b/src/Extensions/Components/BootstrapBlazor.AzureTranslator/Services/AzureTranslatorService.cs @@ -0,0 +1,103 @@ +// 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/ + +using Azure; +using Azure.AI.Translation.Text; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace BootstrapBlazor.Components; + +/// +/// AzureTranslatorService 服务实现类 +/// +/// +/// 构造函数 +/// +/// +/// +class AzureTranslatorService(IOptionsMonitor option, ILogger logger) : IAzureTranslatorService +{ + private readonly IOptionsMonitor _option = option; + + private readonly ILogger _logger = logger; + + /// + /// 文本翻译方法 + /// + /// 目标语言 + /// 输入文本 + /// 源语言 + /// + /// + 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); + IReadOnlyList? ret = null; + try + { + var response = await client.TranslateAsync(targetLanguage, inputText, sourceLanguage, cancellationToken); + ret = response.Value; + } + catch (RequestFailedException ex) + { + _logger.LogError(ex, "TranslateAsync method failed target language: {targetLanguage}, input text: {inputText}", targetLanguage, inputText); + } + return ret ?? new List(); + } + + /// + /// 文本翻译方法 + /// + /// 目标语言 + /// 输入文本集合 + /// 源语言 + /// + /// + 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); + IReadOnlyList? ret = null; + try + { + var response = await client.TranslateAsync(targetLanguage, content, sourceLanguage, cancellationToken); + ret = response.Value; + } + catch (RequestFailedException ex) + { + _logger.LogError(ex, "TranslateAsync method failed target language: {targetLanguage}, content: {inputText}", targetLanguage, content); + } + return ret ?? new List(); + } + + /// + /// 文本翻译方法 + /// + /// 目标语言集合 + /// 输入文本集合 + /// 源语言 + /// + /// + 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); + IReadOnlyList? ret = null; + try + { + var response = await client.TranslateAsync(targetLanguages, content, sourceLanguage: sourceLanguage, cancellationToken: cancellationToken); + ret = response.Value; + } + catch (RequestFailedException ex) + { + _logger.LogError(ex, "TranslateAsync method failed target language: {targetLanguage}, content: {inputText}", targetLanguages, content); + } + return ret ?? new List(); + } +} diff --git a/src/Extensions/Components/BootstrapBlazor.AzureTranslator/Services/IAzureTranslatorService.cs b/src/Extensions/Components/BootstrapBlazor.AzureTranslator/Services/IAzureTranslatorService.cs new file mode 100644 index 00000000000..c5a8f86dde8 --- /dev/null +++ b/src/Extensions/Components/BootstrapBlazor.AzureTranslator/Services/IAzureTranslatorService.cs @@ -0,0 +1,43 @@ +// 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/ + +using Azure.AI.Translation.Text; + +namespace BootstrapBlazor.Components; + +/// +/// IAzureTranslatorService 文本翻译服务 +/// +public interface IAzureTranslatorService +{ + /// + /// 翻译方法 + /// + /// + /// + /// + /// + /// + Task> TranslateAsync(string targetLanguage, string inputText, string? sourceLanguage = null, CancellationToken cancellationToken = default); + + /// + /// 翻译方法 + /// + /// + /// + /// + /// + /// + Task> TranslateAsync(string targetLanguage, IEnumerable content, string? sourceLanguage = null, CancellationToken cancellationToken = default); + + /// + /// 翻译方法 + /// + /// + /// + /// + /// + /// + Task> TranslateAsync(IEnumerable targetLanguages, IEnumerable content, string? sourceLanguage = null, CancellationToken cancellationToken = default); +} diff --git a/src/Extensions/Components/BootstrapBlazor.AzureTranslator/logo.png b/src/Extensions/Components/BootstrapBlazor.AzureTranslator/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..a30290780f1d7fbb242279e1ed64a94be6341d16 GIT binary patch literal 6414 zcmV+p8S&N}_@>`sdfg2neFcr6MRG{xPW0h=w0bz=)_Agoxm=iUBXsh=M1FunP-& z-}Be&+3Kn8uIjEnre}9{>icH8x{mi=y?Rw$$E#Ngw7Icol`RGckD~9kPemCwue4;I zf(gf-wKr^=L69I6!7PZC2o5~$r&3?=}WnP#LZH@sZ0dX3>2XaYbp@t!>3=0Y+#Z+oIpeZ!0* zXQF^5^XPtQYCa63;YMI403u8nPJys#xBv=DqQPt?#J+fXC7T}VyU}(TpdY5osS3poxO? zOa&&LUrysq&#fDpbYVIQL}X)*-#%6VpR8@rWyTjB1>tZQp1-fkl!yXuYB2ZyIsoGb znMtQ*?>jghM$arT0oN756w3r`UdMFBOt=sLC7Vgh^_rTqDC6=+s^OhwbtWJt0Hzc+ z0m35KK%GLO2;;O!wTQxkAol@?41wELUD#OL7Wf zz=aub)xYWT$;ay7Ws6|L~q z#zyErEK>`k9@-tyclK$^fibi4q0hM)DsUqpmVl`j?^EIa-_&SlMjQ1>gVN#icO7;m z2<^gKvQ^~hf8zHC@geOVey3PXyJKsU5IH8W~a~tIAhLBV#GB z@`<#H9XF`_avbc+XZ4m6#{vjJBLH9S9C)BtD>S`Es^oZl_uDC;teg8@$q*KX0m6$dT8fkaC}WmlnfXX>ASP8&?8UF2t~aQlO>21w2Th6oBjR z&xgL}W$2>o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+9}Ib}@> z0Ea1^DTrL2seIX)3Y;u!lml4zA+kueF&dQgbtVpA-G@??v?-mb(8;n!Ie>K^dbqT3 zJKiiA4Nm$x0|&6qvFYm7ouS&>%E(+FrwIT?MQ#ZVzW_ikS5LYG>m#DhPwhr;saUrY3s?%9W1x zB$chebkgEy7%QHZL?{3>0Y24RMH|b<5kVNAykm&vOgr?GOCiZ^fOGQGi|5kYL9M8i z5&#~6I|m-<)dEeI5rH_B0O>&_tw$KbJ(2># Date: Sat, 9 Dec 2023 14:15:30 +0800 Subject: [PATCH 2/6] =?UTF-8?q?chore:=20=E5=A2=9E=E5=8A=A0=E7=BF=BB?= =?UTF-8?q?=E8=AF=91=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BootstrapBlazor.sln | 2 +- src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj | 1 + src/BootstrapBlazor.Server/Extensions/ServicesExtensions.cs | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/BootstrapBlazor.sln b/BootstrapBlazor.sln index 0391a223573..d73a457fcae 100644 --- a/BootstrapBlazor.sln +++ b/BootstrapBlazor.sln @@ -120,7 +120,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "configuration", "configurat exclusion.dic = exclusion.dic EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapBlazor.AzureTranslator", "src\Extensions\Components\BootstrapBlazor.AzureTranslator\BootstrapBlazor.AzureTranslator.csproj", "{D63E50CB-EF78-47BF-9809-003D4574ABF4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BootstrapBlazor.AzureTranslator", "src\Extensions\Components\BootstrapBlazor.AzureTranslator\BootstrapBlazor.AzureTranslator.csproj", "{D63E50CB-EF78-47BF-9809-003D4574ABF4}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj b/src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj index 94c1cd890ea..fd4ff853589 100644 --- a/src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj +++ b/src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj @@ -30,6 +30,7 @@ + diff --git a/src/BootstrapBlazor.Server/Extensions/ServicesExtensions.cs b/src/BootstrapBlazor.Server/Extensions/ServicesExtensions.cs index 3c8ba9a2113..18c3dd6bb95 100644 --- a/src/BootstrapBlazor.Server/Extensions/ServicesExtensions.cs +++ b/src/BootstrapBlazor.Server/Extensions/ServicesExtensions.cs @@ -61,6 +61,9 @@ void Invoke(BootstrapBlazorOptions option) // 增加 AzureOpenAI 服务 services.AddBootstrapBlazorAzureOpenAIService(); + // 增加 AzureTranslator 服务 + services.AddBootstrapBlazorAzureTranslator(); + // 增加 Pdf 导出服务 services.AddBootstrapBlazorHtml2PdfService(); From 9098fd3ba3d76cb4a325dcba4a9fb0900396ecac Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Sat, 9 Dec 2023 14:15:44 +0800 Subject: [PATCH 3/6] =?UTF-8?q?doc:=20=E5=A2=9E=E5=8A=A0=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=E6=BA=90=E7=A0=81=E6=98=A0=E5=B0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor.Server/docs.json | 1 + 1 file changed, 1 insertion(+) diff --git a/src/BootstrapBlazor.Server/docs.json b/src/BootstrapBlazor.Server/docs.json index 5f6ffd02dbc..c5f368bc2ff 100644 --- a/src/BootstrapBlazor.Server/docs.json +++ b/src/BootstrapBlazor.Server/docs.json @@ -191,6 +191,7 @@ "table/tree": "Table\\TablesTree", "table/virtualization": "Table\\TablesVirtualization", "table/wrap": "Table\\TablesWrap", + "translator": "Translators", "js-extensions": "JSRuntimeExtensions", "clipboard-service": "Clipboards", "image-cropper": "ImageCroppers" From 48286232c384df12bfa2a82b980eda71bda55968 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Sat, 9 Dec 2023 14:15:54 +0800 Subject: [PATCH 4/6] =?UTF-8?q?doc:=20=E5=A2=9E=E5=8A=A0=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Samples/Translators.razor | 27 ++++++++ .../Components/Samples/Translators.razor.cs | 65 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/BootstrapBlazor.Server/Components/Samples/Translators.razor create mode 100644 src/BootstrapBlazor.Server/Components/Samples/Translators.razor.cs diff --git a/src/BootstrapBlazor.Server/Components/Samples/Translators.razor b/src/BootstrapBlazor.Server/Components/Samples/Translators.razor new file mode 100644 index 00000000000..37466cbd7b9 --- /dev/null +++ b/src/BootstrapBlazor.Server/Components/Samples/Translators.razor @@ -0,0 +1,27 @@ +@page "/translator" +@inject IStringLocalizer Localizer + +

@Localizer["TranslatorsTitle"]

+

@Localizer["TranslatorsDescription"]

+ + + + + + + + + + + @if (_results.Any()) + { + @foreach (var translation in _results.First().Translations) + { +
@FormatResult(translation)
+ } + } +
+ + diff --git a/src/BootstrapBlazor.Server/Components/Samples/Translators.razor.cs b/src/BootstrapBlazor.Server/Components/Samples/Translators.razor.cs new file mode 100644 index 00000000000..ddc31a57616 --- /dev/null +++ b/src/BootstrapBlazor.Server/Components/Samples/Translators.razor.cs @@ -0,0 +1,65 @@ +// 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/ + +using Azure.AI.Translation.Text; +using System.Globalization; + +namespace BootstrapBlazor.Server.Components.Samples; + +/// +/// 翻译示例 +/// +public partial class Translators +{ + private readonly List _languages = []; + + private List _selectedLanguages = []; + + private static readonly string[] sourceArray = ["zh-CN", "en-US", "ru-RU"]; + + private string _input = ""; + + private IReadOnlyList _results = new List(); + + [Inject] + [NotNull] + private IAzureTranslatorService? TranslatorService { get; set; } + + /// + /// + /// + protected override void OnInitialized() + { + base.OnInitialized(); + + _languages.AddRange(sourceArray.Select(i => new SelectedItem(i, new CultureInfo(i).NativeName))); + _selectedLanguages.AddRange(sourceArray); + } + + private async Task OnClickTranslate() + { + _results = await TranslatorService.TranslateAsync(_selectedLanguages, [_input], "en-US"); + } + + private static string FormatResult(Translation translation) + { + var culture = new CultureInfo(translation.To); + return $"{culture.NativeName}: {translation.Text}"; + } + + /// + /// 获得属性方法 + /// + /// + protected IEnumerable GetMethods() => new MethodItem[] + { + new() + { + Name = nameof(IAzureTranslatorService.TranslateAsync), + Description = Localizer[nameof(IAzureTranslatorService.TranslateAsync)], + Parameters = " - ", + ReturnValue = "IReadOnlyList" + }, + }; +} From 0db9137812349b69e81815fc234cfc3fa983f75e Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Sat, 9 Dec 2023 14:24:20 +0800 Subject: [PATCH 5/6] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=E5=86=85?= =?UTF-8?q?=E7=BD=AE=E6=9C=8D=E5=8A=A1=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/MenusLocalizerExtensions.cs | 21 +++++++++++++++++++ src/BootstrapBlazor.Server/Locales/en.json | 4 +++- src/BootstrapBlazor.Server/Locales/zh.json | 4 +++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/BootstrapBlazor.Server/Extensions/MenusLocalizerExtensions.cs b/src/BootstrapBlazor.Server/Extensions/MenusLocalizerExtensions.cs index b0f15b6b053..4fa4435d006 100644 --- a/src/BootstrapBlazor.Server/Extensions/MenusLocalizerExtensions.cs +++ b/src/BootstrapBlazor.Server/Extensions/MenusLocalizerExtensions.cs @@ -83,6 +83,13 @@ public static List GenerateMenus(this IStringLocalizer Locali }; AddSpeech(item); + item = new DemoMenuItem() + { + Text = Localizer["Services"], + Icon = "fa-fw fa-solid fa-screwdriver-wrench", + }; + AddServices(item); + item = new DemoMenuItem() { Text = Localizer["OtherComponents"], @@ -1291,6 +1298,20 @@ void AddLayout(DemoMenuItem item) AddBadge(item); } + void AddServices(DemoMenuItem item) + { + item.Items = new List + { + new() + { + IsNew = true, + Text = Localizer["AzureTranslator"], + Url = "translator" + } + }; + AddBadge(item); + } + void AddSummary(DemoMenuItem item) { // 计算组件总数 diff --git a/src/BootstrapBlazor.Server/Locales/en.json b/src/BootstrapBlazor.Server/Locales/en.json index fb5cde3dd79..d06928fc842 100644 --- a/src/BootstrapBlazor.Server/Locales/en.json +++ b/src/BootstrapBlazor.Server/Locales/en.json @@ -4528,7 +4528,9 @@ "Clipboard": "Clipboard Service", "CodeEditor": "CodeEditor", "Gantt": "Gantt", - "ImageCropper": "ImageCropper" + "ImageCropper": "ImageCropper", + "Services": "Services", + "AzureTranslator": "AzureTranslator" }, "BootstrapBlazor.Server.Components.Samples.Table.TablesHeader": { "TablesHeaderTitle": "Header grouping function", diff --git a/src/BootstrapBlazor.Server/Locales/zh.json b/src/BootstrapBlazor.Server/Locales/zh.json index 899c8f3c039..0277b05be99 100644 --- a/src/BootstrapBlazor.Server/Locales/zh.json +++ b/src/BootstrapBlazor.Server/Locales/zh.json @@ -4528,7 +4528,9 @@ "Clipboard": "剪切板服务", "CodeEditor": "代码编辑器 CodeEditor", "Gantt": "甘特图 Gantt", - "ImageCropper": "图像裁剪 ImageCropper" + "ImageCropper": "图像裁剪 ImageCropper", + "Services": "内置服务", + "AzureTranslator": "翻译服务 AzureTranslator" }, "BootstrapBlazor.Server.Components.Samples.Table.TablesHeader": { "TablesHeaderTitle": "表头分组功能", From 7739ea0dadfca50ef0fddef87c02601cf0dbc37a Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Sat, 9 Dec 2023 14:37:51 +0800 Subject: [PATCH 6/6] =?UTF-8?q?doc:=20=E5=A2=9E=E5=8A=A0=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E5=8C=96=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Samples/Translators.razor | 3 +++ src/BootstrapBlazor.Server/Locales/en.json | 7 +++++++ src/BootstrapBlazor.Server/Locales/zh.json | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/src/BootstrapBlazor.Server/Components/Samples/Translators.razor b/src/BootstrapBlazor.Server/Components/Samples/Translators.razor index 37466cbd7b9..02e770cf65e 100644 --- a/src/BootstrapBlazor.Server/Components/Samples/Translators.razor +++ b/src/BootstrapBlazor.Server/Components/Samples/Translators.razor @@ -6,6 +6,9 @@ +
@Localizer["TranslatorsInjectService"]
+
services.AddBootstrapBlazorAzureTranslator();
+ diff --git a/src/BootstrapBlazor.Server/Locales/en.json b/src/BootstrapBlazor.Server/Locales/en.json index d06928fc842..6bc140c0ff6 100644 --- a/src/BootstrapBlazor.Server/Locales/en.json +++ b/src/BootstrapBlazor.Server/Locales/en.json @@ -6041,5 +6041,12 @@ "AttributesImageCropperOnBase64Result": "Crop result base64 callback method", "AttributesImageCropperCrop": "Crop, return base64, and execute OnResult/OnBase64Result callback", "AttributesImageCropperCropToStream": "Crop, return Stream" + }, + "BootstrapBlazor.Server.Components.Samples.Translators": { + "TranslatorsTitle": "Azure Translator", + "TranslatorsDescription": "Converts characters or letters of a source language to the corresponding characters or letters of a target language.", + "TranslatorsNormalTitle": "Basic usage", + "TranslatorsNormalIntro": "Call TranslateAsync method for convert target language", + "TranslatorsInjectService": "Inject Service" } } diff --git a/src/BootstrapBlazor.Server/Locales/zh.json b/src/BootstrapBlazor.Server/Locales/zh.json index 0277b05be99..8728608c132 100644 --- a/src/BootstrapBlazor.Server/Locales/zh.json +++ b/src/BootstrapBlazor.Server/Locales/zh.json @@ -6041,5 +6041,12 @@ "AttributesImageCropperOnBase64Result": "剪裁结果 base64 回调方法", "AttributesImageCropperCrop": "剪裁,返回 base64, 并执行 OnResult/OnBase64Result 回调", "AttributesImageCropperCropToStream": "剪裁,返回 Stream" + }, + "BootstrapBlazor.Server.Components.Samples.Translators": { + "TranslatorsTitle": "AzureTranslator 翻译服务", + "TranslatorsDescription": "将源语言的字符或字母转换为目标语言的对应字符或字母", + "TranslatorsNormalTitle": "基础用法", + "TranslatorsNormalIntro": "通过调用 TranslateAsync 进行文本翻译", + "TranslatorsInjectService": "注入服务" } }