🐛 Fixes of translation api
This commit is contained in:
		| @@ -2,5 +2,5 @@ namespace DysonNetwork.Sphere.Translation; | ||||
|  | ||||
| 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 | ||||
| { | ||||
|     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"]!, | ||||
|         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 request = new TextTranslateRequest(); | ||||
|         request.SourceText = text; | ||||
|         request.Source = "auto"; | ||||
|         request.Source = sourceLanguage ?? "auto"; | ||||
|         request.Target = targetLanguage; | ||||
|         request.ProjectId = _projectId; | ||||
|         var response = await client.TextTranslate(request); | ||||
|         return response.TargetText; | ||||
|     } | ||||
|   | ||||
| @@ -12,7 +12,7 @@ namespace DysonNetwork.Sphere.Translation; | ||||
| public class TranslationController(ITranslationProvider provider, ICacheService cache) : ControllerBase | ||||
| { | ||||
|     private const string CacheKeyPrefix = "translation:"; | ||||
|      | ||||
|  | ||||
|     private static string GenerateCacheKey(string text, string targetLanguage) | ||||
|     { | ||||
|         var inputBytes = Encoding.UTF8.GetBytes($"{text}:{targetLanguage}"); | ||||
| @@ -22,27 +22,31 @@ public class TranslationController(ITranslationProvider provider, ICacheService | ||||
|  | ||||
|     [HttpPost] | ||||
|     [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 (currentUser.PerkSubscription is null) | ||||
|             return StatusCode(403, "You need a subscription to use this feature."); | ||||
|          | ||||
|  | ||||
|         // Generate cache key | ||||
|         var cacheKey = GenerateCacheKey(text, targetLanguage); | ||||
|          | ||||
|  | ||||
|         // Try to get from cache first | ||||
|         var (found, cachedResult) = await cache.GetAsyncWithStatus<string>(cacheKey); | ||||
|         if (found && cachedResult != null) | ||||
|             return Ok(cachedResult); | ||||
|              | ||||
|  | ||||
|         // If not in cache, translate and cache the result | ||||
|         var result = await provider.Translate(text, targetLanguage); | ||||
|         if (!string.IsNullOrEmpty(result)) | ||||
|         { | ||||
|             await cache.SetAsync(cacheKey, result, TimeSpan.FromHours(24)); // Cache for 24 hours | ||||
|         } | ||||
|          | ||||
|  | ||||
|         return result; | ||||
|     } | ||||
| } | ||||
| @@ -24,6 +24,7 @@ | ||||
|   "Translation": { | ||||
|     "Provider": "Tencent", | ||||
|     "Region": "ap-hongkong", | ||||
|     "ProjectId": "0", | ||||
|     "SecretId": "", | ||||
|     "SecretKey": "" | ||||
|   }, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user