This commit is contained in:
Nikolaj Olsson 2024-01-31 16:00:53 +01:00
commit 4603e550b0
2 changed files with 15 additions and 3 deletions

View File

@ -17,8 +17,14 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
/// </summary>
string Url { get; }
/// <summary>
/// Represents an error message or description.
/// </summary>
string Error { get; set; }
/// <summary>
/// Gets the maximum number of characters that can be translated at once.
/// </summary>
int MaxCharacters { get; }
/// <summary>
@ -37,8 +43,14 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
List<TranslationPair> GetSupportedTargetLanguages();
/// <summary>
/// Do translation.
/// Translates the given text from the source language to the target language.
/// </summary>
/// <param name="text">The text to be translated.</param>
/// <param name="sourceLanguageCode">The language code of the source language.</param>
/// <param name="targetLanguageCode">The language code of the target language.</param>
/// <param name="cancellationToken">The cancellation token to cancel the translation process.</param>
/// <returns>A task that represents the asynchronous translation operation.
/// The task result contains the translated text.</returns>
Task<string> Translate(string text, string sourceLanguageCode, string targetLanguageCode, CancellationToken cancellationToken);
}
}

View File

@ -13,6 +13,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
public class MyMemoryApi : IAutoTranslator
{
private IDownloader _httpClient;
private readonly SeJsonParser _jsonParser = new SeJsonParser();
public static string StaticName { get; set; } = "MyMemory Translate";
public string Name => StaticName;
@ -212,8 +213,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
var url = $"?langpair={sourceLanguageCode}|{targetLanguageCode}{apiKey}&q={Utilities.UrlEncode(text)}";
var jsonResultString = _httpClient.GetStringAsync(url).Result;
var parser = new SeJsonParser();
var textResult = parser.GetFirstObject(jsonResultString, "translatedText");
var textResult = _jsonParser.GetFirstObject(jsonResultString, "translatedText");
var result = Json.DecodeJsonText(textResult);
try