🐛 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;
|
||||||
}
|
}
|
||||||
|
@@ -12,7 +12,7 @@ namespace DysonNetwork.Sphere.Translation;
|
|||||||
public class TranslationController(ITranslationProvider provider, ICacheService cache) : ControllerBase
|
public class TranslationController(ITranslationProvider provider, ICacheService cache) : ControllerBase
|
||||||
{
|
{
|
||||||
private const string CacheKeyPrefix = "translation:";
|
private const string CacheKeyPrefix = "translation:";
|
||||||
|
|
||||||
private static string GenerateCacheKey(string text, string targetLanguage)
|
private static string GenerateCacheKey(string text, string targetLanguage)
|
||||||
{
|
{
|
||||||
var inputBytes = Encoding.UTF8.GetBytes($"{text}:{targetLanguage}");
|
var inputBytes = Encoding.UTF8.GetBytes($"{text}:{targetLanguage}");
|
||||||
@@ -22,27 +22,31 @@ 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)
|
||||||
return StatusCode(403, "You need a subscription to use this feature.");
|
return StatusCode(403, "You need a subscription to use this feature.");
|
||||||
|
|
||||||
// Generate cache key
|
// Generate cache key
|
||||||
var cacheKey = GenerateCacheKey(text, targetLanguage);
|
var cacheKey = GenerateCacheKey(text, targetLanguage);
|
||||||
|
|
||||||
// Try to get from cache first
|
// Try to get from cache first
|
||||||
var (found, cachedResult) = await cache.GetAsyncWithStatus<string>(cacheKey);
|
var (found, cachedResult) = await cache.GetAsyncWithStatus<string>(cacheKey);
|
||||||
if (found && cachedResult != null)
|
if (found && cachedResult != null)
|
||||||
return Ok(cachedResult);
|
return Ok(cachedResult);
|
||||||
|
|
||||||
// If not in cache, translate and cache the result
|
// If not in cache, translate and cache the result
|
||||||
var result = await provider.Translate(text, targetLanguage);
|
var result = await provider.Translate(text, targetLanguage);
|
||||||
if (!string.IsNullOrEmpty(result))
|
if (!string.IsNullOrEmpty(result))
|
||||||
{
|
{
|
||||||
await cache.SetAsync(cacheKey, result, TimeSpan.FromHours(24)); // Cache for 24 hours
|
await cache.SetAsync(cacheKey, result, TimeSpan.FromHours(24)); // Cache for 24 hours
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -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