Work on translate

This commit is contained in:
Nikolaj Olsson 2024-01-28 20:37:47 +01:00
parent fe1d7ce238
commit b459aa22bb
6 changed files with 29 additions and 2 deletions

View File

@ -976,5 +976,13 @@ namespace Test.Logic
Assert.AreEqual(2, c.G);
Assert.AreEqual(3, c.B);
}
[TestMethod]
public void UrlDecode1()
{
var s = Utilities.UrlDecode("В о\u0442е\u043bе");
Assert.AreEqual("В отеле", s);
}
}
}

View File

@ -50,7 +50,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
var input = "{\"model\": \"gpt-3.5-turbo\",\"messages\": [{ \"role\": \"user\", \"content\": \"Please translate the following text from " + sourceLanguageCode + " to " + targetLanguageCode + ", only write the result: \\n\\n" + Json.EncodeJsonText(text.Trim()) + "\" }]}";
var content = new StringContent(input, Encoding.UTF8);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var result = _httpClient.PostAsync(string.Empty, content).Result;
var result = await _httpClient.PostAsync(string.Empty, content, cancellationToken);
var bytes = await result.Content.ReadAsByteArrayAsync();
var json = Encoding.UTF8.GetString(bytes).Trim();
if (!result.IsSuccessStatusCode)

View File

@ -81,6 +81,13 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
outputText = outputText.Trim('"').Trim();
}
if (!string.IsNullOrEmpty(outputText))
{
outputText = outputText.Replace("<br />", Environment.NewLine);
outputText = outputText.Replace("<br/>", Environment.NewLine);
outputText = outputText.Replace("<br>", Environment.NewLine);
}
return outputText;
}

View File

@ -214,6 +214,7 @@ namespace Nikse.SubtitleEdit.Core.AutoTranslate
var parser = new SeJsonParser();
var textResult = parser.GetFirstObject(jsonResultString, "translatedText");
var result = Json.DecodeJsonText(textResult);
result = Utilities.UrlDecode(result);
return Task.FromResult(result);
}
}

View File

@ -1372,6 +1372,11 @@ namespace Nikse.SubtitleEdit.Core.Common
/// </summary>
public static string UrlDecode(string text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
// pre-process for + sign space formatting since System.Uri doesn't handle it
// plus literals are encoded as %2b normally so this should be safe
text = text.Replace('+', ' ');

View File

@ -10,6 +10,7 @@ using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MessageBox = Nikse.SubtitleEdit.Forms.SeMsgBox.MessageBox;
using Timer = System.Windows.Forms.Timer;
@ -710,6 +711,11 @@ namespace Nikse.SubtitleEdit.Forms.Translate
}
}
}
catch (TaskCanceledException exception)
{
SeLogger.Error(exception);
// ignore
}
catch (Exception exception)
{
HandleError(exception, linesTranslated, engineType);
@ -742,7 +748,7 @@ namespace Nikse.SubtitleEdit.Forms.Translate
labelPleaseWait.Text = LanguageSettings.Current.GoogleTranslate.PleaseWait + $" ({i})";
labelPleaseWait.Refresh();
Application.DoEvents();
System.Threading.Thread.Sleep(1000);
Thread.Sleep(1000);
if (_breakTranslation)
{
break;