diff --git a/DysonNetwork.Sphere/DysonNetwork.Sphere.csproj b/DysonNetwork.Sphere/DysonNetwork.Sphere.csproj index c8c8d6a..582075d 100644 --- a/DysonNetwork.Sphere/DysonNetwork.Sphere.csproj +++ b/DysonNetwork.Sphere/DysonNetwork.Sphere.csproj @@ -59,6 +59,7 @@ + diff --git a/DysonNetwork.Sphere/Startup/ServiceCollectionExtensions.cs b/DysonNetwork.Sphere/Startup/ServiceCollectionExtensions.cs index 5286bdf..ad4356e 100644 --- a/DysonNetwork.Sphere/Startup/ServiceCollectionExtensions.cs +++ b/DysonNetwork.Sphere/Startup/ServiceCollectionExtensions.cs @@ -19,6 +19,7 @@ using DysonNetwork.Shared.GeoIp; using DysonNetwork.Sphere.WebReader; using DysonNetwork.Sphere.Developer; using DysonNetwork.Sphere.Discovery; +using DysonNetwork.Sphere.Translation; namespace DysonNetwork.Sphere.Startup; @@ -166,7 +167,15 @@ public static class ServiceCollectionExtensions services.AddScoped(); services.AddScoped(); services.AddScoped(); - + + var translationProvider = configuration["Translation:Provider"]?.ToLower(); + switch (translationProvider) + { + case "tencent": + services.AddScoped(); + break; + } + return services; } } \ No newline at end of file diff --git a/DysonNetwork.Sphere/Translation/ITranslationProvider.cs b/DysonNetwork.Sphere/Translation/ITranslationProvider.cs new file mode 100644 index 0000000..a2d9c6a --- /dev/null +++ b/DysonNetwork.Sphere/Translation/ITranslationProvider.cs @@ -0,0 +1,6 @@ +namespace DysonNetwork.Sphere.Translation; + +public interface ITranslationProvider +{ + public Task Translate(string text, string targetLanguage); +} \ No newline at end of file diff --git a/DysonNetwork.Sphere/Translation/TencentTranslation.cs b/DysonNetwork.Sphere/Translation/TencentTranslation.cs new file mode 100644 index 0000000..db3af86 --- /dev/null +++ b/DysonNetwork.Sphere/Translation/TencentTranslation.cs @@ -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 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; + } +} \ No newline at end of file diff --git a/DysonNetwork.Sphere/Translation/TranslationController.cs b/DysonNetwork.Sphere/Translation/TranslationController.cs new file mode 100644 index 0000000..4b41835 --- /dev/null +++ b/DysonNetwork.Sphere/Translation/TranslationController.cs @@ -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> 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); + } +} \ No newline at end of file diff --git a/DysonNetwork.Sphere/appsettings.json b/DysonNetwork.Sphere/appsettings.json index 994bdfa..0e35927 100644 --- a/DysonNetwork.Sphere/appsettings.json +++ b/DysonNetwork.Sphere/appsettings.json @@ -29,6 +29,12 @@ "PreferredRegion": "us-east-1" } }, + "Translation": { + "Provider": "Tencent", + "Region": "ap-hongkong", + "SecretId": "", + "SecretKey": "" + }, "KnownProxies": [ "127.0.0.1", "::1"