Skip to content

Commit

Permalink
Handle cases where the source language is not present in BingTranslator
Browse files Browse the repository at this point in the history
  • Loading branch information
d4n3436 committed Sep 27, 2024
1 parent 27e4af7 commit d98b8fc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
3 changes: 0 additions & 3 deletions src/GTranslate/Models/BingTranslationResultModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ internal sealed class BingTranslationResultModel

[JsonPropertyName("inputTransliteration")]
public string? InputTransliteration { get; set; }

[MemberNotNullWhen(true, nameof(DetectedLanguage), nameof(Translations))]
public bool HasTranslations => DetectedLanguage is not null && Translations is not null;
}
16 changes: 11 additions & 5 deletions src/GTranslate/Results/BingTranslationResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GTranslate.Translators;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

Expand All @@ -10,7 +11,7 @@ namespace GTranslate.Results;
public class BingTranslationResult : ITranslationResult<Language>, ITranslationResult
{
internal BingTranslationResult(string translation, string source, Language targetLanguage,
Language sourceLanguage, string? transliteration, string? sourceTransliteration, string? script, float score)
Language? sourceLanguage, string? transliteration, string? sourceTransliteration, string? script, float score)
{
Translation = translation;
Source = source;
Expand All @@ -34,8 +35,9 @@ internal BingTranslationResult(string translation, string source, Language targe
/// <inheritdoc/>
public Language TargetLanguage { get; }

/// <inheritdoc/>
public Language SourceLanguage { get; }
/// <inheritdoc cref="ITranslationResult{TLanguage}.SourceLanguage"/>
/// <remarks>This property returns <see langword="null"/> when the translator can't determine the source language.</remarks>
public Language? SourceLanguage { get; }

/// <summary>
/// Gets the transliteration of the result (<see cref="Translation"/>).
Expand All @@ -62,12 +64,16 @@ internal BingTranslationResult(string translation, string source, Language targe

/// <inheritdoc />
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
ILanguage ITranslationResult<ILanguage>.SourceLanguage => SourceLanguage;
Language ITranslationResult<Language>.SourceLanguage => SourceLanguage ?? throw new NotSupportedException("The source language is not available for this result.");

/// <inheritdoc />
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
ILanguage ITranslationResult<ILanguage>.SourceLanguage => SourceLanguage ?? throw new NotSupportedException("The source language is not available for this result.");

/// <inheritdoc />
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
ILanguage ITranslationResult<ILanguage>.TargetLanguage => TargetLanguage;

/// <inheritdoc/>
public override string ToString() => $"{nameof(Translation)}: '{Translation}', {nameof(TargetLanguage)}: '{TargetLanguage.Name} ({TargetLanguage.ISO6391})', {nameof(SourceLanguage)}: '{SourceLanguage.Name} ({SourceLanguage.ISO6391})', {nameof(Service)}: {Service}";
public override string ToString() => $"{nameof(Translation)}: '{Translation}', {nameof(TargetLanguage)}: '{TargetLanguage.Name} ({TargetLanguage.ISO6391})', {nameof(SourceLanguage)}: '{SourceLanguage?.Name ?? "Unknown"} ({SourceLanguage?.ISO6391 ?? "N/A"})', {nameof(Service)}: {Service}";
}
17 changes: 12 additions & 5 deletions src/GTranslate/Results/BingTransliterationResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GTranslate.Translators;
using System;
using System.Diagnostics;

namespace GTranslate.Results;
Expand All @@ -8,7 +9,8 @@ namespace GTranslate.Results;
/// </summary>
public class BingTransliterationResult : ITransliterationResult<Language>, ITransliterationResult
{
internal BingTransliterationResult(string transliteration, string? sourceTransliteration, string source, Language targetLanguage, Language sourceLanguage, string script)
internal BingTransliterationResult(string transliteration, string? sourceTransliteration,
string source, Language targetLanguage, Language? sourceLanguage, string script)
{
Transliteration = transliteration;
SourceTransliteration = sourceTransliteration;
Expand All @@ -35,8 +37,9 @@ internal BingTransliterationResult(string transliteration, string? sourceTransli
/// <inheritdoc/>
public Language TargetLanguage { get; }

/// <inheritdoc/>
public Language SourceLanguage { get; }
/// <inheritdoc cref="ITransliterationResult{TLanguage}.SourceLanguage"/>
/// <remarks>This property returns <see langword="null"/> when the translator can't determine the source language.</remarks>
public Language? SourceLanguage { get; }

/// <summary>
/// Gets the language script.
Expand All @@ -45,12 +48,16 @@ internal BingTransliterationResult(string transliteration, string? sourceTransli

/// <inheritdoc />
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
ILanguage ITransliterationResult<ILanguage>.SourceLanguage => SourceLanguage;
Language ITransliterationResult<Language>.SourceLanguage => SourceLanguage ?? throw new NotSupportedException("The source language is not available for this result.");

/// <inheritdoc />
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
ILanguage ITransliterationResult<ILanguage>.SourceLanguage => SourceLanguage ?? throw new NotSupportedException("The source language is not available for this result.");

/// <inheritdoc />
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
ILanguage ITransliterationResult<ILanguage>.TargetLanguage => TargetLanguage;

/// <inheritdoc/>
public override string ToString() => $"{nameof(Transliteration)}: '{Transliteration}', {nameof(TargetLanguage)}: '{TargetLanguage.Name} ({TargetLanguage.ISO6391})', {nameof(SourceLanguage)}: '{SourceLanguage.Name} ({SourceLanguage.ISO6391})', {nameof(Service)}: {Service}";
public override string ToString() => $"{nameof(Transliteration)}: '{Transliteration}', {nameof(TargetLanguage)}: '{TargetLanguage.Name} ({TargetLanguage.ISO6391})', {nameof(SourceLanguage)}: '{SourceLanguage?.Name ?? "Unknown"} ({SourceLanguage?.ISO6391 ?? "N/A"})', {nameof(Service)}: {Service}";
}
9 changes: 5 additions & 4 deletions src/GTranslate/Translators/BingTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,18 @@ public async Task<BingTranslationResult> TranslateAsync(string text, ILanguage t
var results = document.Deserialize(BingTranslationResultModelContext.Default.BingTranslationResultModelArray)!;
var result = results[0];

if (!result.HasTranslations)
if (result.Translations is null)
{
throw new TranslatorException("Received an invalid response from the API.");
}

var translation = result.Translations[0];
var transliteration = translation.Transliteration?.Text;
var sourceTransliteration = results.ElementAtOrDefault(1)?.InputTransliteration;
var sourceLanguage = result.DetectedLanguage?.Language ?? fromLanguage?.ISO6391;

return new BingTranslationResult(translation.Text, text, Language.GetLanguage(translation.To), Language.GetLanguage(result.DetectedLanguage.Language),
transliteration, sourceTransliteration, translation.Transliteration?.Script, result.DetectedLanguage.Score ?? 0);
return new BingTranslationResult(translation.Text, text, Language.GetLanguage(translation.To), sourceLanguage is null ? null : Language.GetLanguage(sourceLanguage),
transliteration, sourceTransliteration, translation.Transliteration?.Script, result.DetectedLanguage?.Score ?? 0);
}

/// <summary>
Expand Down Expand Up @@ -191,7 +192,7 @@ public async Task<Language> DetectLanguageAsync(string text)
TranslatorGuards.MaxTextLength(text, MaxTextLength);

var result = await TranslateAsync(text, "en").ConfigureAwait(false);
return result.SourceLanguage;
return result.SourceLanguage ?? throw new TranslatorException("Unable to detect the language of text.");
}

/// <summary>
Expand Down

0 comments on commit d98b8fc

Please sign in to comment.