From 84b33dc9e03bd037773011364722b881558c19e9 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Tue, 9 Jan 2024 20:15:46 +0100 Subject: [PATCH] Work on translate selected lines --- LanguageBaseEnglish.xml | 1 + src/libse/Common/Settings.cs | 8 ++ src/ui/Forms/Main.cs | 182 +++++++----------------- src/ui/Forms/Options/Settings.cs | 1 + src/ui/Forms/Translate/AutoTranslate.cs | 8 +- src/ui/Logic/Language.cs | 1 + src/ui/Logic/LanguageDeserializer.cs | 3 + src/ui/Logic/LanguageStructure.cs | 1 + 8 files changed, 69 insertions(+), 136 deletions(-) diff --git a/LanguageBaseEnglish.xml b/LanguageBaseEnglish.xml index 14d540032..3b758ecca 100644 --- a/LanguageBaseEnglish.xml +++ b/LanguageBaseEnglish.xml @@ -2708,6 +2708,7 @@ can edit in same subtitle file (collaboration) Extract audio (selected lines) Audio to text selected lines ({0}) Toggle brightness (mpv only) + Auto-translate selected lines Translate, custom search 1 Translate, custom search 2 Translate, custom search 3 diff --git a/src/libse/Common/Settings.cs b/src/libse/Common/Settings.cs index 83c53bc61..eff75e960 100644 --- a/src/libse/Common/Settings.cs +++ b/src/libse/Common/Settings.cs @@ -2731,6 +2731,7 @@ $HorzAlign = Center public string MainTranslateGoogleIt { get; set; } public string MainTranslateGoogleTranslateIt { get; set; } public string MainTranslateAuto { get; set; } + public string MainTranslateAutoSelectedLines { get; set; } public string MainTranslateCustomSearch1 { get; set; } public string MainTranslateCustomSearch2 { get; set; } public string MainTranslateCustomSearch3 { get; set; } @@ -11249,6 +11250,12 @@ $HorzAlign = Center shortcuts.MainTranslateAuto = subNode.InnerText; } + subNode = node.SelectSingleNode("MainTranslateAutoSelectedLines"); + if (subNode != null) + { + shortcuts.MainTranslateAutoSelectedLines = subNode.InnerText; + } + subNode = node.SelectSingleNode("MainTranslateCustomSearch1"); if (subNode != null) { @@ -12869,6 +12876,7 @@ $HorzAlign = Center textWriter.WriteElementString("MainTranslateGoogleIt", shortcuts.MainTranslateGoogleIt); textWriter.WriteElementString("MainTranslateGoogleTranslateIt", shortcuts.MainTranslateGoogleTranslateIt); textWriter.WriteElementString("MainTranslateAuto", shortcuts.MainTranslateAuto); + textWriter.WriteElementString("MainTranslateAutoSelectedLines", shortcuts.MainTranslateAutoSelectedLines); textWriter.WriteElementString("MainTranslateCustomSearch1", shortcuts.MainTranslateCustomSearch1); textWriter.WriteElementString("MainTranslateCustomSearch2", shortcuts.MainTranslateCustomSearch2); textWriter.WriteElementString("MainTranslateCustomSearch3", shortcuts.MainTranslateCustomSearch3); diff --git a/src/ui/Forms/Main.cs b/src/ui/Forms/Main.cs index ce78386bb..3bdf6218e 100644 --- a/src/ui/Forms/Main.cs +++ b/src/ui/Forms/Main.cs @@ -26238,6 +26238,7 @@ namespace Nikse.SubtitleEdit.Forms moveTextDownToolStripMenuItem.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.MainListViewColumnTextDown); toolStripMenuItemReverseRightToLeftStartEnd.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.MainEditReverseStartAndEndingForRTL); autotranslateNLLBToolStripMenuItem.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.MainTranslateAuto); + genericTranslateToolStripMenuItem.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.MainTranslateAutoSelectedLines); applyCustomStylesToolStripMenuItem.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.GeneralApplyAssaOverrideTags); setPositionToolStripMenuItem.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.GeneralSetAssaPosition); colorPickerToolStripMenuItem.ShortcutKeys = UiUtil.GetKeys(Configuration.Settings.Shortcuts.GeneralColorPicker); @@ -34967,138 +34968,13 @@ namespace Nikse.SubtitleEdit.Forms private void TranslateSelectedLinesToolStripMenuItemClick(object sender, EventArgs e) { var onlySelectedLines = true; - Translate(onlySelectedLines); + MakeAutoTranslate(onlySelectedLines); } private void TranslateToolStripMenuItemClick(object sender, EventArgs e) { var onlySelectedLines = false; - Translate(onlySelectedLines); - } - - private void Translate(bool onlySelectedLines) - { - if (!IsSubtitleLoaded) - { - DisplaySubtitleNotLoadedMessage(); - return; - } - - bool isOriginalVisible = SubtitleListview1.IsOriginalTextColumnVisible; - ReloadFromSourceView(); - using (var translateDialog = new GenericTranslate()) - { - SaveSubtitleListviewIndices(); - string title = ""; - - if (onlySelectedLines) - { - var selectedLines = new Subtitle(); - foreach (int index in SubtitleListview1.SelectedIndices) - { - selectedLines.Paragraphs.Add(new Paragraph(_subtitle.Paragraphs[index], false)); - } - - title += " - " + _language.SelectedLines; - if (_subtitleOriginal != null) - { - var paragraphs = new List(); - foreach (int index in SubtitleListview1.SelectedIndices) - { - var original = Utilities.GetOriginalParagraph(index, _subtitle.Paragraphs[index], _subtitleOriginal.Paragraphs); - if (original != null) - { - paragraphs.Add(new Paragraph(original, false)); - } - } - - if (paragraphs.Count == selectedLines.Paragraphs.Count) - { - translateDialog.Initialize(new Subtitle(paragraphs), selectedLines, title, GetCurrentEncoding(), GetCurrentSubtitleFormat()); - } - else - { - translateDialog.Initialize(selectedLines, null, title, GetCurrentEncoding(), GetCurrentSubtitleFormat()); - } - } - else - { - translateDialog.Initialize(selectedLines, null, title, GetCurrentEncoding(), GetCurrentSubtitleFormat()); - } - } - else - { - translateDialog.Initialize(_subtitle, null, title, GetCurrentEncoding(), GetCurrentSubtitleFormat()); - } - - if (translateDialog.ShowDialog(this) == DialogResult.OK) - { - _subtitleListViewIndex = -1; - string oldFileName = _fileName; - MakeHistoryForUndo(_language.BeforeGoogleTranslation); - if (onlySelectedLines) - { - // we only update selected lines - int i = 0; - foreach (int index in SubtitleListview1.SelectedIndices) - { - _subtitle.Paragraphs[index].Text = translateDialog.TranslatedSubtitle.Paragraphs[i].Text; - i++; - } - - ShowStatus(_language.SelectedLinesTranslated); - } - else - { - ShowSubtitleTimer.Stop(); - var oldHash = _changeSubtitleHash; - _subtitleOriginal = new Subtitle(_subtitle); - _subtitleOriginalFileName = _fileName; - _fileName = null; - _subtitle.Paragraphs.Clear(); - foreach (var p in translateDialog.TranslatedSubtitle.Paragraphs) - { - _subtitle.Paragraphs.Add(new Paragraph(p)); - } - - SetAssaResolution(_subtitleOriginal); - ShowStatus(_language.SubtitleTranslated); - _changeOriginalSubtitleHash = oldHash; - _changeSubtitleHash = -1; - ShowSubtitleTimer.Start(); - } - - ShowSource(); - - if (!onlySelectedLines) - { - SubtitleListview1.ShowOriginalTextColumn(_languageGeneral.OriginalText); - SubtitleListview1.AutoSizeAllColumns(this); - var oldHash = _changeOriginalSubtitleHash; - SetupOriginalEdit(); - _changeOriginalSubtitleHash = oldHash; - } - - SubtitleListview1.Fill(_subtitle, _subtitleOriginal); - if (!onlySelectedLines) - { - ResetHistory(); - _fileName = translateDialog.GetFileNameWithTargetLanguage(oldFileName, _videoFileName, _subtitleOriginal, GetCurrentSubtitleFormat()); - _converted = true; - } - - RestoreSubtitleListviewIndices(); - - SetTitle(); - SetEncoding(Encoding.UTF8); - if (!isOriginalVisible) - { - toolStripMenuItemShowOriginalInPreview.Checked = false; - Configuration.Settings.General.ShowOriginalAsPreviewIfAvailable = false; - audioVisualizer.Invalidate(); - } - } - } + MakeAutoTranslate(onlySelectedLines); } private void AutotranslateViaCopypasteToolStripMenuItemClick(object sender, EventArgs e) @@ -36269,23 +36145,65 @@ namespace Nikse.SubtitleEdit.Forms bool isOriginalVisible = SubtitleListview1.IsOriginalTextColumnVisible; ReloadFromSourceView(); string title = string.Empty; - using (var autoTranslate = new AutoTranslate(_subtitle, null, title, GetCurrentEncoding())) + var sub = new Subtitle(_subtitle); + Subtitle target = null; + if (onlySelectedLines) + { + title = LanguageSettings.Current.GoogleTranslate.Title + " - " + _language.SelectedLines; + + sub = new Subtitle(); + foreach (int index in SubtitleListview1.SelectedIndices) + { + sub.Paragraphs.Add(new Paragraph(_subtitle.Paragraphs[index], false)); + } + + if (_subtitleOriginal != null) + { + var paragraphs = new List(); + var paragraphsToRemove = new List(); + foreach (int index in SubtitleListview1.SelectedIndices) + { + var original = Utilities.GetOriginalParagraph(index, _subtitle.Paragraphs[index], _subtitleOriginal.Paragraphs); + if (original != null) + { + paragraphs.Add(new Paragraph(original, false)); + } + else + { + paragraphsToRemove.Add(_subtitle.Paragraphs[index]); + } + } + + foreach (var p in paragraphsToRemove) + { + sub.Paragraphs.RemoveAll(x => x.Id == p.Id); + } + + target = sub; + sub = new Subtitle(paragraphs); + } + } + + using (var autoTranslate = new AutoTranslate(sub, target, title, GetCurrentEncoding())) { SaveSubtitleListviewIndices(); if (autoTranslate.ShowDialog(this) == DialogResult.OK) { _subtitleListViewIndex = -1; - string oldFileName = _fileName; + var oldFileName = _fileName; MakeHistoryForUndo(_language.BeforeGoogleTranslation); if (onlySelectedLines) { // we only update selected lines - int i = 0; foreach (int index in SubtitleListview1.SelectedIndices) { - _subtitle.Paragraphs[index].Text = autoTranslate.TranslatedSubtitle.Paragraphs[i].Text; - i++; + var p = _subtitle.Paragraphs[index]; + var t = autoTranslate.TranslatedSubtitle.Paragraphs.FirstOrDefault(x => x.Id == p.Id); + if (t != null) + { + p.Text = t.Text; + } } ShowStatus(_language.SelectedLinesTranslated); diff --git a/src/ui/Forms/Options/Settings.cs b/src/ui/Forms/Options/Settings.cs index 91ec5dccc..37d840479 100644 --- a/src/ui/Forms/Options/Settings.cs +++ b/src/ui/Forms/Options/Settings.cs @@ -1813,6 +1813,7 @@ namespace Nikse.SubtitleEdit.Forms.Options AddNode(translateNode, LanguageSettings.Current.Main.VideoControls.GoogleIt, nameof(Configuration.Settings.Shortcuts.MainTranslateGoogleIt)); AddNode(translateNode, LanguageSettings.Current.Main.VideoControls.GoogleTranslate, nameof(Configuration.Settings.Shortcuts.MainTranslateGoogleTranslateIt)); AddNode(translateNode, LanguageSettings.Current.Main.VideoControls.AutoTranslate, nameof(Configuration.Settings.Shortcuts.MainTranslateAuto), true); + AddNode(translateNode, language.AutoTranslateSelectedLines, nameof(Configuration.Settings.Shortcuts.MainTranslateAutoSelectedLines), true); AddNode(translateNode, language.CustomSearch1, nameof(Configuration.Settings.Shortcuts.MainTranslateCustomSearch1)); AddNode(translateNode, language.CustomSearch2, nameof(Configuration.Settings.Shortcuts.MainTranslateCustomSearch2)); AddNode(translateNode, language.CustomSearch3, nameof(Configuration.Settings.Shortcuts.MainTranslateCustomSearch3)); diff --git a/src/ui/Forms/Translate/AutoTranslate.cs b/src/ui/Forms/Translate/AutoTranslate.cs index b4127a959..1378b6e7a 100644 --- a/src/ui/Forms/Translate/AutoTranslate.cs +++ b/src/ui/Forms/Translate/AutoTranslate.cs @@ -32,7 +32,7 @@ namespace Nikse.SubtitleEdit.Forms.Translate private bool _translationInProgress; private CancellationTokenSource _cancellationTokenSource; - public AutoTranslate(Subtitle subtitle, Subtitle selectedLines, string title, Encoding encoding) + public AutoTranslate(Subtitle subtitle, Subtitle targetLines, string title, Encoding encoding) { UiUtil.PreInitialize(this); InitializeComponent(); @@ -89,15 +89,15 @@ namespace Nikse.SubtitleEdit.Forms.Translate labelPleaseWait.Visible = false; progressBar1.Visible = false; - if (selectedLines != null) + if (targetLines != null) { - TranslatedSubtitle = new Subtitle(selectedLines); + TranslatedSubtitle = new Subtitle(targetLines, false); TranslatedSubtitle.Renumber(); subtitleListViewTarget.Fill(TranslatedSubtitle); } else { - TranslatedSubtitle = new Subtitle(_subtitle); + TranslatedSubtitle = new Subtitle(_subtitle, false); foreach (var paragraph in TranslatedSubtitle.Paragraphs) { paragraph.Text = string.Empty; diff --git a/src/ui/Logic/Language.cs b/src/ui/Logic/Language.cs index 436c39d47..d04475542 100644 --- a/src/ui/Logic/Language.cs +++ b/src/ui/Logic/Language.cs @@ -3066,6 +3066,7 @@ can edit in same subtitle file (collaboration)", AudioToTextSelectedLinesX = "Audio to text selected lines ({0})", AudioExtractSelectedLines = "Extract audio (selected lines)", VideoToggleBrightness = "Toggle brightness (mpv only)", + AutoTranslateSelectedLines = "Auto-translate selected lines", CustomSearch1 = "Translate, custom search 1", CustomSearch2 = "Translate, custom search 2", CustomSearch3 = "Translate, custom search 3", diff --git a/src/ui/Logic/LanguageDeserializer.cs b/src/ui/Logic/LanguageDeserializer.cs index bc80a3905..ef47c4255 100644 --- a/src/ui/Logic/LanguageDeserializer.cs +++ b/src/ui/Logic/LanguageDeserializer.cs @@ -7411,6 +7411,9 @@ namespace Nikse.SubtitleEdit.Logic case "Settings/VideoToggleBrightness": language.Settings.VideoToggleBrightness = reader.Value; break; + case "Settings/AutoTranslateSelectedLines": + language.Settings.AutoTranslateSelectedLines = reader.Value; + break; case "Settings/CustomSearch1": language.Settings.CustomSearch1 = reader.Value; break; diff --git a/src/ui/Logic/LanguageStructure.cs b/src/ui/Logic/LanguageStructure.cs index 9275a1401..926acb2ba 100644 --- a/src/ui/Logic/LanguageStructure.cs +++ b/src/ui/Logic/LanguageStructure.cs @@ -2884,6 +2884,7 @@ public string AudioExtractSelectedLines { get; set; } public string AudioToTextSelectedLinesX { get; set; } public string VideoToggleBrightness { get; set; } + public string AutoTranslateSelectedLines { get; set; } public string CustomSearch1 { get; set; } public string CustomSearch2 { get; set; } public string CustomSearch3 { get; set; }