Skip to content

Commit

Permalink
refactored translator service API design
Browse files Browse the repository at this point in the history
  • Loading branch information
valdisiljuconoks committed Dec 28, 2024
1 parent 5be72f4 commit 8cdd040
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -831,8 +831,8 @@
axios
.get('api/service/auto-translate?inputText=' + this.model.currentResource.invariant + '&targetLanguage=' + this.model.currentResource.language)
.then(response => {
if (response) {
this.model.currentResource.translation = response.data;
if (response && response.data.isSuccessful) {
this.model.currentResource.translation = response.data.result;
}
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@
axios
.get('../api/service/auto-translate?inputText=' + this.currentResource.invariant + '&targetLanguage=' + this.currentResource.language)
.then(response => {
if (response) {
this.currentResource.translation = response.data;
if (response && response.data.isSuccessful) {
this.currentResource.translation = response.data.result;
}
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,12 @@ public async Task<IActionResult> AutoTranslate(string inputText, string targetLa
return BadRequest("Translator service is not configured.");
}

var resultText = await translator.TranslateAsync(inputText, targetLanguage);
var result = await translator.TranslateAsync(
inputText,
CultureInfo.GetCultureInfo(targetLanguage),
CultureInfo.InvariantCulture);

return Ok(resultText);
return Ok(result);
}

private LocalizationResourceApiModel PrepareViewModel(string keyword)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using System.Threading.Tasks;

namespace DbLocalizationProvider.Abstractions;
Expand All @@ -11,7 +12,8 @@ public interface ITranslatorService
/// Translates text from one language to another.
/// </summary>
/// <param name="inputText">Text to translate.</param>
/// <param name="targetLanguage">Target language code to translate text to.</param>
/// <param name="targetLanguage">Target language to translate text to.</param>
/// <param name="sourceLanguage">Source language to translate text from.</param>
/// <returns>Translated text if all is good; otherwise <c>null</c>.</returns>
Task<string> TranslateAsync(string inputText, string targetLanguage);
Task<TranslationResult> TranslateAsync(string inputText, CultureInfo targetLanguage, CultureInfo sourceLanguage);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace DbLocalizationProvider.Abstractions;

/// <summary>
/// Result of the translation operation from AI service.
/// </summary>
/// <param name="IsSuccessful">Is operation successful?</param>
/// <param name="Result">Translated text to target language.</param>
/// <param name="Error">Filled in with problem details if any.</param>
public record TranslationResult(bool IsSuccessful, string? Result, Problem? Error = null)
{
/// <summary>
/// Translation was successful.
/// </summary>
/// <param name="text">Translated text to target language.</param>
/// <returns>Successful translation result.</returns>
public static TranslationResult Ok(string text)
{
return new TranslationResult(true, text);
}

/// <summary>
/// Translation was failing.
/// </summary>
/// <param name="message">Error details if translation failed.</param>
/// <param name="details">More details about an error.</param>
/// <returns>Failed translation result.</returns>
public static TranslationResult Failed(string message, string? details = null)
{
return new TranslationResult(false, null, new Problem(message, details));
}
}

/// <summary>
/// More details about failed translation.
/// </summary>
/// <param name="Message">Message about an error.</param>
/// <param name="Details">More information about an error.</param>
public record Problem(string? Message, string? Details)
{
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Globalization;
using Azure;
using Azure.AI.Translation.Text;
using DbLocalizationProvider.Abstractions;
Expand Down Expand Up @@ -26,24 +27,34 @@ public CognitiveServiceTranslator(IOptions<CognitiveServicesOptions> options, IL
}

/// <inheritdoc />
public async Task<string?> TranslateAsync(string inputText, string targetLanguage)
public async Task<TranslationResult> TranslateAsync(string inputText, CultureInfo targetLanguage, CultureInfo sourceLanguage)
{
AzureKeyCredential credential = new(_options.AccessKey);
TextTranslationClient client = new(credential, _options.Region);

try
{
var response = await client.TranslateAsync(targetLanguage, inputText).ConfigureAwait(false);
var response = await client.TranslateAsync(targetLanguage.Name, inputText).ConfigureAwait(false);
var translations = response.Value;
var translation = translations.FirstOrDefault();

return translation?.Translations?.FirstOrDefault()?.Text;
if (translations == null)
{
return TranslationResult.Failed("Failed to translate. Result from Azure Cognitive Service is null.");
}

var translation = translations[0]!;

if (translation.Translations == null || translation.Translations?.Count == 0)
{
return TranslationResult.Failed("Failed to translate. Translations from Azure Cognitive Service is null.");
}

return TranslationResult.Ok(translation.Translations[0].Text);
}
catch (RequestFailedException exception)
{
_logger.Error($"Failed to auto-translate to `{targetLanguage}`.", exception);
return TranslationResult.Failed($"Failed to translate. {exception.Message}");
}

return default;
}
}

0 comments on commit 8cdd040

Please sign in to comment.