Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LakeishaKowalczyk committed Sep 20, 2024
1 parent c0c7e87 commit ccecad4
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 79 deletions.
2 changes: 1 addition & 1 deletion LanguageBaseEnglish.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ We leverage the intrinsic rhythm of the image.</CreateSimpleChainingToolTip>
<MergeSplitStrategy>Split/merge handling</MergeSplitStrategy>
<Delay>Delay between server calls</Delay>
<MaxBytes>Maximum bytes in each server call</MaxBytes>
<MaxRetries>Maximum number of retries(unlimited=-1)</MaxRetries>
<MaxMerges>Max merge attempts(always=-1,never=0)</MaxMerges>
<PromptX>Prompt for {0}</PromptX>
<TranslateLinesSeparately>Translate each line separately</TranslateLinesSeparately>
</GoogleTranslate>
Expand Down
6 changes: 3 additions & 3 deletions src/libse/Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2317,10 +2317,10 @@ public static Settings CustomDeserialize(string fileName)
settings.Tools.AutoTranslateMaxBytes = Convert.ToInt32(subNode.InnerText, CultureInfo.InvariantCulture);
}

subNode = node.SelectSingleNode("AutoTranslateMaxRetries");
subNode = node.SelectSingleNode("AutoTranslateMaxMerges");
if (subNode != null)
{
settings.Tools.AutoTranslateMaxRetries = Convert.ToInt32(subNode.InnerText, CultureInfo.InvariantCulture);
settings.Tools.AutoTranslateMaxMerges = Convert.ToInt32(subNode.InnerText, CultureInfo.InvariantCulture);
}

subNode = node.SelectSingleNode("AutoTranslateStrategy");
Expand Down Expand Up @@ -9033,7 +9033,7 @@ public static string CustomSerialize(Settings settings)
textWriter.WriteElementString("AnthropicApiModel", settings.Tools.AnthropicApiModel);
textWriter.WriteElementString("AutoTranslateDelaySeconds", settings.Tools.AutoTranslateDelaySeconds.ToString(CultureInfo.InvariantCulture));
textWriter.WriteElementString("AutoTranslateMaxBytes", settings.Tools.AutoTranslateMaxBytes.ToString(CultureInfo.InvariantCulture));
textWriter.WriteElementString("AutoTranslateMaxRetries", settings.Tools.AutoTranslateMaxRetries.ToString(CultureInfo.InvariantCulture));
textWriter.WriteElementString("AutoTranslateMaxMerges", settings.Tools.AutoTranslateMaxMerges.ToString(CultureInfo.InvariantCulture));
textWriter.WriteElementString("AutoTranslateStrategy", settings.Tools.AutoTranslateStrategy);
textWriter.WriteElementString("GeminiProApiKey", settings.Tools.GeminiProApiKey);
textWriter.WriteElementString("TextToSpeechEngine", settings.Tools.TextToSpeechEngine);
Expand Down
4 changes: 2 additions & 2 deletions src/libse/Settings/ToolsSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class ToolsSettings
public string AnthropicApiModel { get; set; }
public int AutoTranslateDelaySeconds { get; set; }
public int AutoTranslateMaxBytes { get; set; }
public int AutoTranslateMaxRetries { get; set; }
public int AutoTranslateMaxMerges { get; set; }
public string AutoTranslateStrategy { get; set; }
public string GeminiProApiKey { get; set; }
public string TextToSpeechEngine { get; set; }
Expand Down Expand Up @@ -490,7 +490,7 @@ public ToolsSettings()
AnthropicApiModel = AnthropicTranslate.Models[0];
TextToSpeechAzureRegion = "westeurope";
AutoTranslateMaxBytes = 2000;
AutoTranslateMaxRetries = -1;
AutoTranslateMaxMerges = -1;
TextToSpeechAddToVideoFile = true;
TranslateAllowSplit = true;
TranslateViaCopyPasteAutoCopyToClipboard = true;
Expand Down
34 changes: 4 additions & 30 deletions src/ui/Forms/Translate/AutoTranslate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,8 @@ private async void buttonTranslate_Click(object sender, EventArgs e)
timerUpdate.Start();
var linesTranslated = 0;


int maxMergeErrorCount = Configuration.Settings.Tools.AutoTranslateMaxRetries;
// Max merge attempts(always=-1,never=0), fallback to forceSingleLineMode after exceeding the number of attempts
int maxMergeErrorCount = Configuration.Settings.Tools.AutoTranslateMaxMerges;

var forceSingleLineMode = Configuration.Settings.Tools.AutoTranslateStrategy == TranslateStrategy.TranslateEachLineSeparately.ToString() ||
_autoTranslator.Name == NoLanguageLeftBehindApi.StaticName || // NLLB seems to miss some text...
Expand Down Expand Up @@ -868,25 +868,9 @@ private async void buttonTranslate_Click(object sender, EventArgs e)
break;
}




var linesMergedAndTranslated = await MergeAndSplitHelper.MergeAndTranslateIfPossible(_subtitle, TranslatedSubtitle, source, target, index, _autoTranslator, forceSingleLineMode, _cancellationTokenSource.Token);
Application.DoEvents();


// 当翻译结果为空时重试 MaxRetryAttempts 次
/*
int retryAttempts = 0;
var linesMergedAndTranslated = 0;
do
{
linesMergedAndTranslated = await MergeAndSplitHelper.MergeAndTranslateIfPossible(_subtitle, TranslatedSubtitle, source, target, index, _autoTranslator, forceSingleLineMode, _cancellationTokenSource.Token);
Application.DoEvents();
retryAttempts++;
await Task.Delay(1000);
} while (linesMergedAndTranslated <= 0 && (maxRetryAttempts == -1 || retryAttempts < maxRetryAttempts)); */

if (_breakTranslation)
{
break;
Expand All @@ -900,7 +884,6 @@ private async void buttonTranslate_Click(object sender, EventArgs e)
continue;
}

// maxMergeErrorCount =-1 屏蔽单行模式 =0 等价于 Translate Each Line Separately Strategy 分别翻译每一行 >0 一次错误次数后转单行模式
if (!forceSingleLineMode)
{
if (maxMergeErrorCount < 0)
Expand All @@ -916,19 +899,11 @@ private async void buttonTranslate_Click(object sender, EventArgs e)
mergeErrorCount++;
}


// 强制多行模式下,不自动回退到单行模式
//if (forceMergeMode)
//{
// continue;
//}

var p = _subtitle.Paragraphs[index];
var f = new Formatting();
var unformattedText = f.SetTagsAndReturnTrimmed(p.Text, source.Code);

var translation = await _autoTranslator.Translate(unformattedText, source.Code, target.Code, _cancellationTokenSource.Token);

var translation = await _autoTranslator.Translate(unformattedText, source.Code, target.Code, _cancellationTokenSource.Token);


if (_breakTranslation)
Expand All @@ -951,10 +926,9 @@ private async void buttonTranslate_Click(object sender, EventArgs e)
_translationProgressIndex = index;
_translationProgressDirty = true;
progressBar1.Value = index;
// Proceed to the next line only after successfully obtaining the translation to avoid intermittent translation results
if (!string.IsNullOrWhiteSpace(translation))
{
// 调试用
//MessageBox.Show("Max Retry Attempts: s" + translation + "s", "Debug Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
linesTranslated++;
index++;
}
Expand Down
64 changes: 32 additions & 32 deletions src/ui/Forms/Translate/AutoTranslateSettings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions src/ui/Forms/Translate/AutoTranslateSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public AutoTranslateSettings(Type engineType, string engineName)
Text = LanguageSettings.Current.General.Advanced;
labelDelay.Text = LanguageSettings.Current.GoogleTranslate.Delay;
labelMaxBytes.Text = LanguageSettings.Current.GoogleTranslate.MaxBytes;
labelMaxRetries.Text = LanguageSettings.Current.GoogleTranslate.MaxRetries; // 设置最大重试次数标签的文本
labelMaxMerges.Text = LanguageSettings.Current.GoogleTranslate.MaxMerges;
labelParagraphHandling.Text = LanguageSettings.Current.GoogleTranslate.LineMergeHandling;
labelPrompt.Text = string.Format(LanguageSettings.Current.GoogleTranslate.PromptX, engineName);
buttonOk.Text = LanguageSettings.Current.General.Ok;
Expand All @@ -36,17 +36,12 @@ public AutoTranslateSettings(Type engineType, string engineName)
}
nikseUpDownMaxBytes.Value = Configuration.Settings.Tools.AutoTranslateMaxBytes;

// 检查并设置 MaxRetries
/*if (Configuration.Settings.Tools.AutoTranslateMaxRetries < -1)
{
Configuration.Settings.Tools.AutoTranslateMaxRetries = new ToolsSettings().AutoTranslateMaxRetries;
}*/
nikseUpDownMaxRetries.Value = Configuration.Settings.Tools.AutoTranslateMaxRetries;
nikseUpDownMaxMerges.Value = Configuration.Settings.Tools.AutoTranslateMaxMerges;

comboBoxParagraphHandling.Left = labelParagraphHandling.Right + 4;
nikseUpDownDelay.Left = labelDelay.Right + 4;
nikseUpDownMaxBytes.Left = labelMaxBytes.Right + 4;
nikseUpDownMaxRetries.Left = labelMaxRetries.Right + 4; // Align new control
nikseUpDownMaxMerges.Left = labelMaxMerges.Right + 4;

if (_engineType == typeof(ChatGptTranslate))
{
Expand Down Expand Up @@ -135,7 +130,7 @@ private void buttonOk_Click(object sender, EventArgs e)

Configuration.Settings.Tools.AutoTranslateDelaySeconds = (int)nikseUpDownDelay.Value;
Configuration.Settings.Tools.AutoTranslateMaxBytes = (int)nikseUpDownMaxBytes.Value;
Configuration.Settings.Tools.AutoTranslateMaxRetries = (int)nikseUpDownMaxRetries.Value; // Save MaxRetries
Configuration.Settings.Tools.AutoTranslateMaxMerges = (int)nikseUpDownMaxMerges.Value;

if (_engineType == typeof(ChatGptTranslate))
{
Expand Down
2 changes: 1 addition & 1 deletion src/ui/Logic/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ public Language()
ReTranslateCurrentLine = "Re-translate current line",
Delay = "Delay between server calls",
MaxBytes = "Maximum bytes in each server call",
MaxRetries = "Maximum number of retries(unlimited=-1)",
MaxMerges = "Max merge attempts(never=0,always=-1)",

MergeSplitStrategy = "Split/merge handling",
PromptX = "Prompt for {0}",
Expand Down
2 changes: 1 addition & 1 deletion src/ui/Logic/LanguageStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ public class GoogleTranslate
public string MergeSplitStrategy { get; set; }
public string Delay { get; set; }
public string MaxBytes { get; set; }
public string MaxRetries { get; set; }
public string MaxMerges { get; set; }
public string PromptX { get; set; }
public string TranslateLinesSeparately { get; set; }
}
Expand Down

0 comments on commit ccecad4

Please sign in to comment.