Integrate SeJsonParser as class field in MyMemoryApi

In the MyMemoryApi class, SeJsonParser is now instantiated as a class field instead of a local variable in the GetTranslatedTextAsync method. This change was implemented to simplify the retrieval of translated text from JSON results.
This commit is contained in:
Ivandro Jao 2024-01-29 22:01:08 +00:00
parent 8ecdad90ad
commit c2a9faed0a

View File

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