Initial commit
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using DeepL;
|
||||
using DeepL.Model;
|
||||
using FictionArchive.Service.TranslationService.Models;
|
||||
using Language = FictionArchive.Common.Enums.Language;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Services.TranslationEngines.DeepLTranslate;
|
||||
|
||||
public class DeepLTranslationAdapater : ITranslationEngineAdapter
|
||||
{
|
||||
private readonly DeepLClient _deepLClient;
|
||||
private readonly ILogger<DeepLTranslationAdapater> _logger;
|
||||
|
||||
private const string DisplayName = "DeepL";
|
||||
private const string Key = "deepl";
|
||||
|
||||
public DeepLTranslationAdapater(DeepLClient deepLClient, ILogger<DeepLTranslationAdapater> logger)
|
||||
{
|
||||
_deepLClient = deepLClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public TranslationEngineDescriptor Descriptor
|
||||
{
|
||||
get
|
||||
{
|
||||
return new TranslationEngineDescriptor()
|
||||
{
|
||||
DisplayName = DisplayName,
|
||||
Key = Key,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string?> GetTranslation(string body, Language from, Language to)
|
||||
{
|
||||
TextResult translationResult = await _deepLClient.TranslateTextAsync(body, GetLanguageCode(from), GetLanguageCode(to));
|
||||
_logger.LogInformation("Translated text. Usage statistics: CHARACTERS BILLED {TranslationResultBilledCharacters}", translationResult.BilledCharacters);
|
||||
return translationResult.Text;
|
||||
}
|
||||
|
||||
private string GetLanguageCode(Language language)
|
||||
{
|
||||
return language switch
|
||||
{
|
||||
Language.En => LanguageCode.EnglishAmerican,
|
||||
Language.Kr => LanguageCode.Korean,
|
||||
Language.Ch => LanguageCode.Chinese,
|
||||
Language.Ja => LanguageCode.Japanese
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user