🐛 Fixes of translation api
This commit is contained in:
@@ -2,5 +2,5 @@ namespace DysonNetwork.Sphere.Translation;
|
|||||||
|
|
||||||
public interface ITranslationProvider
|
public interface ITranslationProvider
|
||||||
{
|
{
|
||||||
public Task<string> Translate(string text, string targetLanguage);
|
public Task<string> Translate(string text, string targetLanguage, string? sourceLanguage = null);
|
||||||
}
|
}
|
@@ -7,19 +7,21 @@ namespace DysonNetwork.Sphere.Translation;
|
|||||||
public class TencentTranslation(IConfiguration configuration) : ITranslationProvider
|
public class TencentTranslation(IConfiguration configuration) : ITranslationProvider
|
||||||
{
|
{
|
||||||
private readonly string _region = configuration["Translation:Region"]!;
|
private readonly string _region = configuration["Translation:Region"]!;
|
||||||
private readonly Credential _apiCredential = new Credential
|
private readonly long _projectId = long.Parse(configuration["Translation:ProjectId"] ?? "0");
|
||||||
|
private readonly Credential _apiCredential = new()
|
||||||
{
|
{
|
||||||
SecretId = configuration["Translation:SecretId"]!,
|
SecretId = configuration["Translation:SecretId"]!,
|
||||||
SecretKey = configuration["Translation:SecretKey"]!
|
SecretKey = configuration["Translation:SecretKey"]!
|
||||||
};
|
};
|
||||||
|
|
||||||
public async Task<string> Translate(string text, string targetLanguage)
|
public async Task<string> Translate(string text, string targetLanguage, string? sourceLanguage = null)
|
||||||
{
|
{
|
||||||
var client = new TmtClient(_apiCredential, _region);
|
var client = new TmtClient(_apiCredential, _region);
|
||||||
var request = new TextTranslateRequest();
|
var request = new TextTranslateRequest();
|
||||||
request.SourceText = text;
|
request.SourceText = text;
|
||||||
request.Source = "auto";
|
request.Source = sourceLanguage ?? "auto";
|
||||||
request.Target = targetLanguage;
|
request.Target = targetLanguage;
|
||||||
|
request.ProjectId = _projectId;
|
||||||
var response = await client.TextTranslate(request);
|
var response = await client.TextTranslate(request);
|
||||||
return response.TargetText;
|
return response.TargetText;
|
||||||
}
|
}
|
||||||
|
@@ -22,7 +22,11 @@ public class TranslationController(ITranslationProvider provider, ICacheService
|
|||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<ActionResult<string>> Translate([FromBody] string text, [FromQuery(Name = "lang")] string targetLanguage)
|
public async Task<ActionResult<string>> Translate(
|
||||||
|
[FromBody] string text,
|
||||||
|
[FromQuery(Name = "from")] string targetLanguage,
|
||||||
|
[FromQuery(Name = "to")] string? sourceLanguage
|
||||||
|
)
|
||||||
{
|
{
|
||||||
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
|
||||||
if (currentUser.PerkSubscription is null)
|
if (currentUser.PerkSubscription is null)
|
||||||
|
@@ -24,6 +24,7 @@
|
|||||||
"Translation": {
|
"Translation": {
|
||||||
"Provider": "Tencent",
|
"Provider": "Tencent",
|
||||||
"Region": "ap-hongkong",
|
"Region": "ap-hongkong",
|
||||||
|
"ProjectId": "0",
|
||||||
"SecretId": "",
|
"SecretId": "",
|
||||||
"SecretKey": ""
|
"SecretKey": ""
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user