-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(AzureTranslator): add AzureTranslator service (#2519)
* feat: 增加 Translator 服务 * chore: 增加翻译服务 * doc: 增加翻译源码映射 * doc: 增加翻译服务示例 * chore: 更新内置服务菜单 * doc: 增加本地化资源
- Loading branch information
Showing
15 changed files
with
375 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/BootstrapBlazor.Server/Components/Samples/Translators.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
@page "/translator" | ||
@inject IStringLocalizer<Translators> Localizer | ||
|
||
<h3>@Localizer["TranslatorsTitle"]</h3> | ||
<h4>@Localizer["TranslatorsDescription"]</h4> | ||
|
||
<PackageTips Name="BootstrapBlazor.AzureTranslator" /> | ||
|
||
<div class="mt-3">@Localizer["TranslatorsInjectService"]</div> | ||
<Pre class="no-highlight mt-3">services.AddBootstrapBlazorAzureTranslator();</Pre> | ||
|
||
<DemoBlock Title="@Localizer["TranslatorsNormalTitle"]" | ||
Introduction="@Localizer["TranslatorsNormalIntro"]" | ||
Name="Normal"> | ||
<CheckboxList Items="_languages" @bind-Value="_selectedLanguages" class="mb-3"></CheckboxList> | ||
<BootstrapInputGroup> | ||
<BootstrapInput @bind-Value="@_input"></BootstrapInput> | ||
<Button Icon="fa-solid fa-language" Text="翻译" OnClick="OnClickTranslate"></Button> | ||
</BootstrapInputGroup> | ||
|
||
@if (_results.Any()) | ||
{ | ||
@foreach (var translation in _results.First().Translations) | ||
{ | ||
<div class="mt-3">@FormatResult(translation)</div> | ||
} | ||
} | ||
</DemoBlock> | ||
|
||
<MethodTable Items="@GetMethods()" /> |
65 changes: 65 additions & 0 deletions
65
src/BootstrapBlazor.Server/Components/Samples/Translators.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// 翻译示例 | ||
/// </summary> | ||
public partial class Translators | ||
{ | ||
private readonly List<SelectedItem> _languages = []; | ||
|
||
private List<string> _selectedLanguages = []; | ||
|
||
private static readonly string[] sourceArray = ["zh-CN", "en-US", "ru-RU"]; | ||
|
||
private string _input = ""; | ||
|
||
private IReadOnlyList<TranslatedTextItem> _results = new List<TranslatedTextItem>(); | ||
|
||
[Inject] | ||
[NotNull] | ||
private IAzureTranslatorService? TranslatorService { get; set; } | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
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}"; | ||
} | ||
|
||
/// <summary> | ||
/// 获得属性方法 | ||
/// </summary> | ||
/// <returns></returns> | ||
protected IEnumerable<MethodItem> GetMethods() => new MethodItem[] | ||
{ | ||
new() | ||
{ | ||
Name = nameof(IAzureTranslatorService.TranslateAsync), | ||
Description = Localizer[nameof(IAzureTranslatorService.TranslateAsync)], | ||
Parameters = " - ", | ||
ReturnValue = "IReadOnlyList<TranslatedTextItem>" | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Extensions/Components/BootstrapBlazor.AzureTranslator/AzureTranslatorOption.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// AzureTranslatorOption 配置类 | ||
/// </summary> | ||
public class AzureTranslatorOption | ||
{ | ||
/// <summary> | ||
/// 获得/设置 订阅 Key 由 Azure 提供 | ||
/// </summary> | ||
[NotNull] | ||
public string? Key { get; set; } | ||
|
||
/// <summary> | ||
/// 获得/设置 Location/Region 描述 由 Azure 提供 | ||
/// </summary> | ||
[NotNull] | ||
public string? Region { get; set; } | ||
|
||
/// <summary> | ||
/// 获得/设置 超时时长 默认 0 未设置 | ||
/// </summary> | ||
public int Timeout { get; set; } | ||
} |
18 changes: 18 additions & 0 deletions
18
...ensions/Components/BootstrapBlazor.AzureTranslator/BootstrapBlazor.AzureTranslator.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Version>8.0.0</Version> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<PackageTags>Bootstrap Blazor Azure WebAssembly wasm Translator Components</PackageTags> | ||
<Description>Bootstrap UI components extensions of Azure Translator</Description> | ||
<RootNamespace>BootstrapBlazor.Components</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.AI.Translation.Text" Version="1.0.0-beta.1" /> | ||
<PackageReference Include="BootstrapBlazor" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
35 changes: 35 additions & 0 deletions
35
...ions/Components/BootstrapBlazor.AzureTranslator/Extensions/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// BootstrapBlazor 服务扩展类 | ||
/// </summary> | ||
public static class ServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// 增加 语音识别服务 | ||
/// </summary> | ||
/// <param name="services"></param> | ||
/// <param name="configOptions"></param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddBootstrapBlazorAzureTranslator(this IServiceCollection services, Action< | ||
AzureTranslatorOption>? configOptions = null) | ||
{ | ||
services.AddHttpClient(); | ||
|
||
services.AddSingleton<IAzureTranslatorService, AzureTranslatorService>(); | ||
services.AddOptionsMonitor<AzureTranslatorOption>(); | ||
|
||
services.Configure<AzureTranslatorOption>(option => | ||
{ | ||
configOptions?.Invoke(option); | ||
}); | ||
return services; | ||
} | ||
} |
Oops, something went wrong.