Translation

This commit is contained in:
2025-07-31 15:02:46 +08:00
parent fd1c47196d
commit 6b1dda41bc
6 changed files with 70 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
namespace DysonNetwork.Sphere.Translation;
public interface ITranslationProvider
{
public Task<string> Translate(string text, string targetLanguage);
}

View File

@@ -0,0 +1,26 @@
using TencentCloud.Common;
using TencentCloud.Tmt.V20180321;
using TencentCloud.Tmt.V20180321.Models;
namespace DysonNetwork.Sphere.Translation;
public class TencentTranslation(IConfiguration configuration) : ITranslationProvider
{
private readonly string _region = configuration["Translation:Region"]!;
private readonly Credential _apiCredential = new Credential
{
SecretId = configuration["Translation:SecretId"]!,
SecretKey = configuration["Translation:SecretKey"]!
};
public async Task<string> Translate(string text, string targetLanguage)
{
var client = new TmtClient(_apiCredential, _region);
var request = new TextTranslateRequest();
request.SourceText = text;
request.Source = "auto";
request.Target = targetLanguage;
var response = await client.TextTranslate(request);
return response.TargetText;
}
}

View File

@@ -0,0 +1,21 @@
using DysonNetwork.Pass.Account;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace DysonNetwork.Sphere.Translation;
[ApiController]
[Route("translate")]
public class TranslationController(ITranslationProvider provider) : ControllerBase
{
[HttpPost]
[Authorize]
public async Task<ActionResult<string>> Translate([FromBody] string text, [FromQuery] string targetLanguage)
{
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
if (currentUser.PerkSubscription is null)
return StatusCode(403, "You need a subscription to use this feature.");
return await provider.Translate(text, targetLanguage);
}
}