[FA-misc] Translation engine work
This commit is contained in:
@@ -2,6 +2,7 @@ using DeepL;
|
||||
using DeepL.Model;
|
||||
using FictionArchive.Service.TranslationService.Models;
|
||||
using FictionArchive.Service.TranslationService.Models.Enums;
|
||||
using Polly;
|
||||
using Language = FictionArchive.Common.Enums.Language;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Services.TranslationEngines.DeepLTranslate;
|
||||
@@ -9,34 +10,38 @@ namespace FictionArchive.Service.TranslationService.Services.TranslationEngines.
|
||||
public class DeepLTranslationEngine : ITranslationEngine
|
||||
{
|
||||
private readonly DeepLClient _deepLClient;
|
||||
private readonly ResiliencePipeline _pipeline;
|
||||
private readonly ILogger<DeepLTranslationEngine> _logger;
|
||||
|
||||
private const string DisplayName = "DeepL";
|
||||
private const string Key = "deepl";
|
||||
|
||||
public DeepLTranslationEngine(DeepLClient deepLClient, ILogger<DeepLTranslationEngine> logger)
|
||||
public DeepLTranslationEngine(
|
||||
DeepLClient deepLClient,
|
||||
ResiliencePipeline pipeline,
|
||||
ILogger<DeepLTranslationEngine> logger)
|
||||
{
|
||||
_deepLClient = deepLClient;
|
||||
_pipeline = pipeline;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public TranslationEngineDescriptor Descriptor
|
||||
public TranslationEngineDescriptor Descriptor => new()
|
||||
{
|
||||
get
|
||||
{
|
||||
return new TranslationEngineDescriptor()
|
||||
{
|
||||
DisplayName = DisplayName,
|
||||
Key = Key,
|
||||
};
|
||||
}
|
||||
}
|
||||
DisplayName = DisplayName,
|
||||
Key = Key,
|
||||
};
|
||||
|
||||
public async Task<TranslationResult> 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 new TranslationResult()
|
||||
TextResult translationResult = await _pipeline.ExecuteAsync(
|
||||
async _ => await _deepLClient.TranslateTextAsync(body, GetLanguageCode(from), GetLanguageCode(to)));
|
||||
|
||||
_logger.LogInformation(
|
||||
"Translated text. Usage statistics: CHARACTERS BILLED {TranslationResultBilledCharacters}",
|
||||
translationResult.BilledCharacters);
|
||||
|
||||
return new TranslationResult
|
||||
{
|
||||
OriginalText = body,
|
||||
From = from,
|
||||
@@ -59,4 +64,4 @@ public class DeepLTranslationEngine : ITranslationEngine
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(language), language, null)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user