-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Azure translation service. Requires adding your own key to a priv…
…ate appsettings JSON file to translate locally with it.
- Loading branch information
Showing
9 changed files
with
175 additions
and
7 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
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,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace AntDesign.Docs.Build.CLI | ||
{ | ||
public class AzureTranslateOptions | ||
{ | ||
public string Key { get; set; } | ||
|
||
public string Region { get; set; } | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
site/AntDesign.Docs.Build.CLI/Services/Translation/AzureTranslate.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,90 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Options; | ||
using YamlDotNet.Core.Tokens; | ||
|
||
namespace AntDesign.Docs.Build.CLI.Services.Translation | ||
{ | ||
public class AzureTranslate : ITranslate | ||
{ | ||
private readonly HttpClient _client; | ||
|
||
private readonly IOptionsSnapshot<AzureTranslateOptions> _options; | ||
|
||
public AzureTranslate(HttpClient client, IOptionsSnapshot<AzureTranslateOptions> options) | ||
{ | ||
_client = client; | ||
_options = options; | ||
} | ||
|
||
public Task BackupTranslations(bool onlyKeepUsed = true) | ||
{ | ||
throw new NotImplementedException("Not the best design, but this class doesn't implement this. It is meant for the cache wrapper class."); | ||
} | ||
|
||
public async Task<string> TranslateText(string text, string to, string from = "auto") | ||
{ | ||
if (string.IsNullOrWhiteSpace(_options.Value.Key) || string.IsNullOrWhiteSpace(text)) | ||
{ | ||
return text; | ||
} | ||
|
||
using var request = new HttpRequestMessage(); | ||
|
||
request.Headers.Add("Ocp-Apim-Subscription-Key", _options.Value.Key); | ||
|
||
if (!string.IsNullOrWhiteSpace(_options.Value.Region)) | ||
{ | ||
request.Headers.Add("Ocp-Apim-Subscription-Region", _options.Value.Region); | ||
} | ||
|
||
request.Method = HttpMethod.Post; | ||
request.Content = RequestContent(text); | ||
request.RequestUri = BuildRequestUri(from, to); | ||
|
||
var response = await _client.SendAsync(request); | ||
if (response.StatusCode == HttpStatusCode.TooManyRequests) | ||
{ | ||
throw new TranslationTooManyRequestsException(); | ||
} | ||
|
||
var responseContent = await response.Content.ReadAsStringAsync(); | ||
var deserialized = JsonSerializer.Deserialize<IEnumerable<AzureTranslationResponse>>(responseContent, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); | ||
|
||
return deserialized.SingleOrDefault()?.Translations.SingleOrDefault()?.Text ?? text; | ||
} | ||
|
||
private Uri BuildRequestUri(string from, string to) | ||
{ | ||
var url = _client.BaseAddress + $"translate?api-version=3.0&to={to}"; | ||
if (from != "auto") | ||
{ | ||
url += $"&from={from}"; | ||
} | ||
|
||
return new Uri(url); | ||
} | ||
|
||
private static HttpContent RequestContent(string text) | ||
{ | ||
var body = new object[] { | ||
new { | ||
Text = text | ||
} | ||
}; | ||
|
||
return new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json"); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
site/AntDesign.Docs.Build.CLI/Services/Translation/AzureTranslationResponse.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,20 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace AntDesign.Docs.Build.CLI.Services.Translation | ||
{ | ||
public class AzureTranslationResponse | ||
{ | ||
public IEnumerable<Translation> Translations { get; set; } | ||
|
||
public class Translation | ||
{ | ||
public string To { get; set; } | ||
|
||
public string Text { get; set; } | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"AzureTranslate": { | ||
"Key": "", | ||
"Region": "" | ||
} | ||
} |